I am working on a instant search function, currently i am having trouble passing the variable from JS to PHP file. I am also unsure about what to do once i have the results from the query. Any help would be fantastic. This is my current standing.
ERROR
Undefined index: partialSearch in \php\search.php on line 4
test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AJAX SEARCH</title>
<link rel="stylesheet" href="stylesheets/base.css">
<link rel="stylesheet" href="stylesheets/skeleton.css">
<link rel="stylesheet" href="stylesheets/layout.css">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function search(partialSearch){
$.ajax({url:"PHP/search.php",data: partialSearch,success:function(result){
$("#results").html(result);
}});
};
</script>
</head>
<body>
<div class="container">
<div class="one-third column index">
<h3>Search Our Site</h3>
<p>Simply type into the search bar below the video, article you are looking for.</p>
<input type="text" name="partialSearch"onkeyup="search(this.value)"/>
<div id="results"></div>
</div>
</div>
</body>
</html>
search.php
<?php
include 'config.php';
$partialSearch = $_POST['partialSearch'];
$stmt = $mysqli->prepare("SELECT Name FROM videos WHERE Name LIKE ? ");
$stmt->bind_param('s',$partialSearch);
$stmt->execute();
$stmt->bind_result($Name);
while ($row = $stmt->fetch()) {
$searchResults[] = $Name;
echo "<div>".$searchResults."</div>";
}
?>
You should change
data: partialSearch
to
data: {partialSearch: partialSearch} // or {"partialSearch": partialSearch}, which is the same
Where partialSearch is the data's index name.
1.) You did not tell jQuery to post, e.g.
$.ajax({ type: "POST", ... });
You can also use the shorthand ".post" :
$.post{url:"PHP/search.php",data: partialSearch,success:function(result){ $("#results").html(result);
2.) The PHP thinks of a named POST variable. So your partialSearch must be an object like so
partialSearch = { partialSearch : "I AM your variable, NOT the object holding me !!!" }
By default, $.ajax calls sends a GET request, so your $_POST is not valid in your .php file, unless you specify a ..type:"POST".. variable in your $.ajax(.. settings.
Secondly, you need to change this:
$.ajax({url:"PHP/search.php",data: partialSearch,success:function(result){
to this:
$.ajax({url:"PHP/search.php",type:"POST",data:{partialSearch:partialSearch},success:function(result){
It's perfectly ok to pass an object of variables to send.
Related
I am trying to send JSON data from page1 on submit button click and try to receive this data dynamically from page2 using AJAX and print the data in console. I don't know the proper syntax to do this. One suggested code which is not appropriate. The code is given:
page1:
<?php
if(isset($_POST["submit"])){
$x = "ok";
echo json_encode($x);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>page1</title>
</head>
<body>
<p>This is page is sending json data on submit button press</p>
<form method="post">
<input type="submit" name="submit">
</form>
</body>
</html>
page2:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</head>
<body>
<p>Get json data from page1 dynamically using ajax</p>
<script>
setInterval(checkVariableValue, 5000);
function checkVariableValue() {
$.ajax({
method: 'POST',
url: 'page1.php',
datatype: 'json',
success: function(data) {
console.log(data);
}
});
}
</script>
</body>
</html>
What should I write to make it works properly?
You can do like this
session_start();
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST["submit"])){
$value = 'I am test'; //can be any value
$_SESSION['key'] = $value;
} else if($_SERVER['REQUEST_METHOD']=='POST')){
echo $_SESSION['key'];
}
At the moment if I run this script, it creates the html page as expected, although I'm having trouble, having it take in to account variables, for instance the $_GET request for example.
This is inside speech marks, and is sent to a new page on my website using file_put_contents, my aim is to have the variable code defined on the page where it's sent from.
In short; at page A I have the code that creates the new file, at page A I could do something like /directory/to/page/?SET=Hello+ from+stack and it would create the new file, and where it says var code = ' '; I want it to instead be the get request, so it would be var code = 'Hello from stack';
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script> var code = '<?php echo $php_variable; ?>';</script>
// This is what I'm having trouble with,
</head>
<body>
<div class='container'>
<pre class='code-sample'>
<div class='heading'>CODE</div>
<div class='code-wrap'>
<code></code>
</div>
</pre>
</div>
</body>
</html>
This is the code I use to create the file;
$file = 'it.html';
$data = "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script> var code = 'I WANT THIS TO BE THE VARIABLE $GET I DEFINED FROM PAGE A';</script>
</head>
<body>
<div class='container'>
<pre class='code-sample'>
<div class='heading'>CODE</div>
<div class='code-wrap'>
<code></code>
</div>
</pre>
</div>
";
file_put_contents($file, $data);
Assuming you have $GET['SET'] containing a string then all you have to remember is that when using arrays in a double quoted string you either reference the array as
$_GET[SET] // without the quotes around the index name
Or
{$_GET['SET']} // wrap the array in {}, this is my preference
So this should do what you want
$_GET['SET'] = 'Hello from stack'; // just here for testing
$file = 'it.html';
$data = "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script> var code = '{$_GET['SET']}';</script>
</head>
<body>
<div class='container'>
<pre class='code-sample'>
<div class='heading'>CODE</div>
<div class='code-wrap'>
<code></code>
</div>
</pre>
</div>
";
Results
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script> var code = 'Hello from stack';</script>
</head>
<body>
<div class='container'>
<pre class='code-sample'>
<div class='heading'>CODE</div>
<div class='code-wrap'>
<code></code>
</div>
</pre>
</div>
Here is my JS function:
function checkError() {
var field = 'error';
var url = window.location.href;
document.write('test');
window.alert('please work');
if(url.indexOf('?' + field + '=') != -1)
document.write('The username and password do not match. Do not use your full email.');
return true;
}
and then in my body paragraph I have:
<?php echo '<script> checkError();</script>' ?>
It doesn't have any errors calling it. But the function does nothing on my page. Any thoughts? I've tried putting the JS script in the page and in a JS file and correctly called for its inclusion.
Full script:
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login</title>
<script type="text/JavaScript" src="js/functions.js"></script>
<link href="stylesheets/mainStyle.css" rel="stylesheet" type="text/css">
<link href="stylesheets/formStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include('header.php'); ?>
<div id="mainContent">
<h1>Member Login</h1>
<div id="mainParaText">
<?php echo '<script> checkError();</script>' ?>
</div>
</body>
</html>
TURNS OUT JS Function is UNDEFINED. Ugh, can't figure out why (thought I fixed this problem a while back lol)
try like this:
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login</title>
<script type="text/JavaScript" src="js/functions.js"></script>
<link href="stylesheets/mainStyle.css" rel="stylesheet" type="text/css">
<link href="stylesheets/formStyle.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function checkError(){
var field = 'error';
var url = window.location.href;
document.write('test');
window.alert('please work');
if(url.indexOf('?' + field + '=') != -1)
document.write('The username and password do not match. Do not use your full email.');
return true;
}
<?php
echo "checkError();";
?>
</script>
</head>
<body>
<?php include('header.php'); ?>
<div id="mainContent">
<h1>Member Login</h1>
<div id="mainParaText">
<?php echo '<script> checkError();</script>' ?>
</div>
</body>
</html>
JS is client side and PHP is server side.
That means that everything in PHP code will be processed on server and then "echoed" to your browser where you get undefined error.
When debugging this you should always check source code in the browser first so you see exactly what your server echoed.
I'm guessing a bit but try with double quotes instead of single.
And not related to the question ...
You are checking username and password with JS? How exactly will you do this? With ajax call back to the server? If you check with JS that means password should be in the source code somewhere and that is NOT secure. Username / pass validation should always be made on serverside (either with ajax request or usual submit form).
I am trying to remove all the links from a parsed site which has then had a div removed and placed in the main code. The problem is that I am trying to remove all the 'href' links in the extracted div but am unable to get anywhere. I have tried using 'CSS' and works but only in chrome and I have to use IE. I have looked at 'php simple html dom' parser to see if i could do it before the file is saved but can't get anything to work. so my last resort is to use 'jquery' but the problem is that the links to remove is being extracted from the file and is not directly in the code. If anyone could help me I would really appreciate it. below is the code I am using.
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<meta http-equiv="content-language" content="en">
<meta name="viewport" content="width=500" />
<title>example News</title>
<link rel="stylesheet" type="text/css" href="site/wwwRand1.css">
<?php
include( 'site/simple_html_dom.php');
$html=file_get_html( 'http://example.com/home.php');
$html->save('site/result.htm')
?>
<script type="text/javascript" src="site/jquery.js"></script>
<script type="text/javascript">
$('document').ready(function() {
$('#postsArea').load('site/result.htm #postsArea');
});
</script>
</head>
<body>
<div id="wrap">
<div id="postsArea"></div>
</div>
</body>
Just remove the href attribute from the <a /> tags
$("#postsArea").load( "site/result.htm", function() {
$("#postsArea a").removeAttr("href")
});
If you still want the tags to appear clickable...
$("#postsArea a").removeAttr("href").css("cursor","pointer");
Hope this helps
Try this:
$('document').ready(function () {
$.ajax({
url : 'site/result.htm',
dataType: 'html',
success : function(html){
$html = $(html).find(a).attr("href", "");
$("#postsArea").html($html);
},
error : function(){
$("#postArea").html("<div>No Data Found.</div>");
}
})
});
Here's how I did it:
$(function() {
$('a[href]').each(function() {
var link = jQuery(this);
link.after(jQuery('<span/>').text(link.text()));
link.remove();
});
});
It replaces Foo with <span>Foo</span>.
Lately I try to make a script which grab news from my facebook wall and post on my website.
With some research and some edit resulted this: example
Now, I want to put this script in the middle slider of this page
Connections are all right (js/css/script), but doesn't appear.
This is the sequence of slider part: (In slider are displayed also news manually introduced in database.)
<div id="middle_article">
<div class="jcarousel-prev-vertical" style="left:63px;"></div>
<div class="jcarousel-next-vertical" style="left:63px;"></div>
<ul id="stiri" class="jcarousel jcarousel-skin-tango">
<?php
$result_other_news = mysql_query("SELECT * FROM `stiri` WHERE `status` = '1' AND `lang` = 'ro' ORDER BY `data` DESC LIMIT 1,10");
while($row_other_news = mysql_fetch_array($result_other_news)){
$link_stire = 'http://www.aepado.ro/stiri/'.friendly_url($row_other_news['titlu']).'-'.$row_other_news['id'].'.html';
?>
<li>
<?php echo substr($row_other_news['titlu'], 0, 75); ?>
</li>
<?php } ?>
</ul>
</div>
Head part of my index page (correspondence with my problem):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="http://aepado.ro/css/jquery.neosmart.fb.wall.css" rel="stylesheet" type="text/css"/>
<script src="http://aepado.ro/js/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="http://aepado.ro/js/jquery.neosmart.fb.wall.js" type="text/javascript"> </script>
<script type="text/javascript">
function(){
$('#example1').fbWall({ id:'***',accessToken:'***'});
});
</script>
And, this is sequence to display script:
<div id="example1"></div>
Finally, i will thank you in advance, and if you need some more information i will gave you with pleasure.
Best regards,
Cosmin
did you initialize jcarousel?
$(function(){
('#mycarousel').jcarousel();
});