2015-02-06 88 views
0

攜帶$ 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> 
+0

請提供不清楚 – 2015-02-06 06:59:17

+0

一些information..little位,你可以發佈您的Ajax代碼和PHP文件? – 2015-02-06 06:59:41

+0

我收錄了它,但這是我從教程中得到的。 – RJC 2015-02-06 07:03:20

回答

1

試試這個

在的search.php文件中添加以下代碼接受ID從index.php文件

<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> 

在index.php文件中使用Ajax請求一起發送$ id變量值。

<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> 

在HTML中增加一個輸入隱藏字段的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> 
+0

虛幻的,它的工作。我理解php的部分,但我並不瞭解你在ajax部分做了什麼,這使得它帶有id。 – RJC 2015-02-06 14:40:15

0

請註明行動= '的search.php'在形式上然後它會重定向到search.php中。 ..在那裏你會得到$ id ...

+0

這沒有奏效。 – RJC 2015-02-06 07:26:50

+0

你想要在index.php上提交表單,並想從那裏提交值到search.php是嗎? – 2015-02-06 07:34:16

+0

是的,但這是使用AJAX。這是一個自動過程,當我在表單中放置action ='search.php'時,它對它沒有任何影響。無論我在search.php中引用$ id多少次,它都不起作用。 – RJC 2015-02-06 07:36:59

相關問題