Reputation: 37
I recently found this tutorial http://www.infotuts.com/create-ajax-based-search-system-php-jquery/ and have noticed that all the results are displayed in search.php while search.php is called through ajax and displayed in index.php. Whenever I tried to integrate it into my website, my index.php contains a variable of $id which is shown in this url: (index.php?id=1) My problem is, I can't seem to get it to know what my $id is. I am unable to change the query in search.php to what I like as a result of this.
$query = mysql_query("select * from userdetail where name like '{$term}%'", $connection);
Here is the original query above me, however I want to have it dependent on the id of the page, so... :
$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
The only problem is, that search.php has no idea what my $id variable is, even when I load it from index.php it does not work. Can anyone help?
The full code can be seen in the tutorial, but I can show you guys here. Search.php
<?php
$connection = mysql_connect('localhost', 'root', '12345');
$db = mysql_select_db('userdata', $connection);
$term = strip_tags(substr($_POST['searchit'],0, 100));
$term = mysql_escape_string($term); // Attack Prevention
if($term=="")
echo "Enter Something to search";
else{
$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
$string = '';
if (mysql_num_rows($query)){
while($row = mysql_fetch_assoc($query)){
$string .= "<b>".$row['name']."</b> - ";
$string .= $row['email']."</a>";
$string .= "<br/>\n";
}
}else{
$string = "No matches found!";
}
echo $string;
}
?>
Here is index.php
<html>
<head>
<title>Ajax Based Search- InfoTuts</title>
<?php
$id = ((int)$_GET["id"]);
?>
<style type="text/css" >
#main {
padding: 10px;
margin: 100px;
margin-left: 300px;
color: Green;
border: 1px dotted;
width: 520px;
}
#display_results {
color: red;
background: #CCCCFF;
}
</style>
<script type="text/javascript "src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$("#search_results").slideUp();
$("#button_find").click(function(event){
event.preventDefault();
search_ajax_way();
});
$("#search_query").keyup(function(event){
event.preventDefault();
search_ajax_way();
});
});
function search_ajax_way(){
$("#search_results").show();
var search_this=$("#search_query").val();
$.post("search.php", {searchit : search_this}, function(data){
$("#display_results").html(data);
})
}
</script>
</head>
<body>
<div id="main">
<h1>Ajax Based Search System- InfoTuts</h1>
<form id="searchform" method="post">
<label>Enter</label>
<input type="text" name="search_query" id="search_query" placeholder="What You Are Looking For?" size="50"/>
<input type="submit" value="Search" id="button_find" />
</form>
<div id="display_results"></div>
</div>
</body>
</html>
Upvotes: 0
Views: 148
Reputation: 951
Try this
In search.php file add the following code to accept the id from index.php file
<code>
$term = mysql_escape_string($term); // Attack Prevention
$id = $_POST['id'];
if(!empty($id)) {
$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
} else {
$query = mysql_query("select * from userdetail where name like '{$term}%'", $connection);
}
if($term=="")
echo "Enter Something to search";
</code>
In the index.php file send the $id variable value along with the ajax request.
<code>
function search_ajax_way(){
$("#search_results").show();
var search_this=$("#search_query").val();
var id_val=$("#id").val();
$.post("search.php", {searchit : search_this, id : id_val}, function(data){ $("#display_results").html(data);}
</code>
In the html add a input hidden field for id
<code>
<form id="searchform" method="post">
<label>Enter</label>
<input type="text" name="search_query" id="search_query" placeholder="What You Are Looking For?" size="50"/>
<input type='hidden' value='<?php echo $id; ?>' id='id'/>
<input type="submit" value="Search" id="button_find" />
</form>
</code>
Upvotes: 1
Reputation: 1164
in
Please mention action='search.php' in form then it will redirect to search.php...and there you will get $id...
Upvotes: 0