我試圖將每個Ajax自動填充結果與特定的URL(「登陸頁面」)鏈接起來。將JSON答案的AJAX自動填充答案鏈接到特定的URL /網頁
網址:www.aebli.com
,在右上角的搜索欄,嘗試用「H」或「豪」 Ajax的代碼搜索方框:
<script type="text/javascript" src="js/dimensions.js"></script>
<script type="text/javascript" src="js/autocomplete.js"></script>
<script type="text/javascript">
$(function(){
setAutoComplete("searchField", "results", "php/autocomplete.php?part=");
});
</script>
這裏的文件「autocomplete.php」其中選擇特定的表格形式列出MySQL數據庫:
<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("dreampix")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$result = mysql_query("SELECT name FROM treffer");
while ($row = mysql_fetch_assoc($result)) {
$colors[]=$row['name'];
}
mysql_free_result($result);
mysql_close($link);
// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
// initialize the results array
$results = array();
// search colors
foreach($colors as $color)
{
// if it starts with 'part' add to results
if(strpos($color, $_GET['part']) === 0){
$results[] = $color;
}
}
// return the array as json with PHP 5.2
echo json_encode($results);
}
?>
正如你可以看到www.aebli.com
,搜索現場工作,嘗試用「豪」。 MySQL-Database-Table僅包含兩行:id和一些條目,如「hochzeit」。如何將「AJAX自動填充結果」與<a href="#">
「連接」到特定着陸頁,如「hochzeit」.php的aebli.com/searchresluts
?
我希望爲「豪」的用戶搜索,得到一個列表中可以點擊結果打開目標網頁的一個..
您應該使用SQL查詢中的WHERE clausule過濾結果。它比選擇所有行並在PHP中手動過濾更快。 (但要注意SQL注入) – 2011-05-06 16:46:45