攜帶$ id變量我最近發現這個教程http://www.infotuts.com/create-ajax-based-search-system-php-jquery/和已經注意到,所有的結果都顯示在search.php中的search.php同時通過AJAX調用,並在index.php中顯示。每當我試圖把它整合到我的網站,我的index.php包含在該網址顯示的$ id的變量:(?的index.php ID = 1)我的問題是,我似乎無法得到它知道我的$ id是什麼。我無法將search.php中的查詢更改爲我喜歡的結果。在另一個文件中
$query = mysql_query("select * from userdetail where name like '{$term}%'", $connection);
這裏是我上面原來的查詢,但是我想擁有它依賴於網頁的ID,所以...:
$query = mysql_query("select * from userdetail where id = '$id' AND name like '{$term}%'", $connection);
唯一的問題是,的search.php不知道我的$ id變量是什麼,即使當我從index.php加載時它不起作用。誰能幫忙?
完整的代碼可以在本教程中可以看出,但在這裏我可以告訴你的傢伙。 的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;
}
?>
這裏是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>
請提供不清楚 – 2015-02-06 06:59:17
一些information..little位,你可以發佈您的Ajax代碼和PHP文件? – 2015-02-06 06:59:41
我收錄了它,但這是我從教程中得到的。 – RJC 2015-02-06 07:03:20