我是jQuery的新手,並試圖在文本框中設置一個簡單的自動完成。我的代碼如下:PHP作爲源不工作的Jquery自動完成
HTML:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$("#searchstr").autocomplete(
{
source:'searchjson.php',
})
});
</script>
<?php
include('code.php') ;
show_header();
?>
<title>CodeLib</title>
</head>
<body>
<div><h3>Welcome to CodeLib.com.au</h3><div>
<h4>Articles</h4>
<form action="index.php" method="get">
<div class="ui-widget" >Search:(4 chars min) <input name="searchstr" id="searchstr" type="text" /></div>
</form>
</body>
和我的PHP代碼:
<?php
include("code.php");
// if the 'term' variable is not sent with the request, exit
if (!isset($_REQUEST['searchstr']))
exit;
open_db();
// query the database table for zip codes that match 'term'
$rs = mysql_query('select article_title from articles where article_title like "'. mysql_real_escape_string($_REQUEST['searchstr']) .'%" limit 10');
// loop through each zipcode returned and format the response for jQuery
$data = array();
$str = "";
if ($rs && mysql_num_rows($rs))
{
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
$data[] = array('label' => $row['article_title']);
}
}
// jQuery wants JSON data
echo json_encode($data);
flush();
?>
幾點需要注意:
我已經測試使用腳本固定值並更改來源如下。 source: availableTags
和JS固定值分配 - 做工精細
測試與查詢字符串傳遞一個字符串的PHP和我得到的結果爲:
[{"label":"Simple HTML rich text editor for your web page"}]
但問題是,當我給你,我沒有得到源代碼到php代碼。任何人都可以給我一個線索?
非常感謝。
* PSA:*了'mysql_ *'功能已廢棄在PHP 5.5(http://php.net/manual/en/faq.databases.php#faq .databases.mysql.deprecated)。不建議您編寫新的代碼,因爲這會阻止您將來升級。相反,請使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –
請發佈什麼searchjson.php返回 –
你知道它在哪裏出錯嗎?它是否將AJAX請求成功發送到PHP代碼? PHP代碼是否會拋出任何錯誤? – MajorCaiger