你在哪里下载这个例子?
http://jqueryui.com/demos/autocomplete/#remote
jQuery没有使PHP可用,所以我不知道如何构建数据或如何使用它。
我没有在页面上看到下载按钮或有关如何构建PHP以与jQuery UI集成的任何说明,我不确定为什么他们的网站不会让开发人员轻松。
答案 0 :(得分:1)
以下是使用自动完成功能的常规aspx中的Page_Load示例。 GetSCACs方法只返回表示JSON数组的字符串。
protected void Page_Load(object sender, EventArgs e)
{
// Clear out the buffer
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
// Do not cache response
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// Set the content type and encoding for JSON
Response.ContentType = "application/json";
Response.ContentEncoding = Encoding.UTF8;
string query = Request["term"];
string scacs = GetSCACs(query);
Response.Write(scacs);
// Flush the response buffer
Response.Flush();
// Complete the request. NOTE: Do not use Response.End() here,
// because it throws a ThreadAbortException, which cannot be caught!
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
答案 1 :(得分:1)
来自概述:
数据源可以是:
- 包含本地数据的数组
- 一个String,指定一个URL
- a Callback
在这种特定情况下,设置使用第二个选项:
当使用String时, 自动完成插件需要这样 string指向一个URL资源 将返回JSON数据。它可以开启 相同的主机或不同的主机 (必须提供JSONP)。请求 参数“term”被添加到那个 URL。数据本身可以在 与本地数据格式相同 如上所述。
所以,它并不重要,但这里是search.php返回的JSON。这应该满足您的抱怨“我不知道如何构建数据或如何使用它。”
[ { "id": "Ficedula hypoleuca", "label": "Eurasian Pied Flycatcher", "value": "Eurasian Pied Flycatcher" }, { "id": "Muscicapa striata", "label": "Spotted Flycatcher", "value": "Spotted Flycatcher" }, { "id": "Branta canadensis", "label": "Greater Canada Goose", "value": "Greater Canada Goose" }, { "id": "Haematopus ostralegus", "label": "Eurasian Oystercatcher", "value": "Eurasian Oystercatcher" }, { "id": "Aythya marila", "label": "Greater Scaup", "value": "Greater Scaup" }, { "id": "Corvus corone", "label": "Carrion Crow", "value": "Carrion Crow" }, { "id": "Sylvia atricapilla", "label": "Blackcap", "value": "Blackcap" }, { "id": "Hydroprogne caspia", "label": "Caspian Tern", "value": "Caspian Tern" }, { "id": "Bubulcus ibis", "label": "Cattle Egret", "value": "Cattle Egret" }, { "id": "Aythya valisineria", "label": "Canvasback", "value": "Canvasback" }, { "id": "Aythya affinis", "label": "Lesser Scaup", "value": "Lesser Scaup" }, { "id": "Anas falcata", "label": "Falcated Duck", "value": "Falcated Duck" } ]
但至于“如何”...让PHP输出一个JSON字符串,只需使用json_encode($arr)
就像:
$arr = array(
"foo" => "bar",
"baz" => "true",
"thinger" => "thing"
);
答案 2 :(得分:0)
我认为我找到了很好的资源 http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/
以下是该网站的文件
<?php
//connection information
$host = "localhost";
$user = "root";
$password = "your_password_here";
$database = "test";
$param = $_GET["term"];
//make connection
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
//query the database
$query = mysql_query("SELECT * FROM friends WHERE name REGEXP '^$param'");
//build array of results
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$friends[$x] = array("name" => $row["name"]);
}
//echo JSON to page
$response = $_GET["callback"] . "(" . json_encode($friends) . ")";
echo $response;
mysql_close($server);
?>