Php 关于autosuggest jquery插件的noob问题
我想用这个插件为我的文本字段做一些自动提示 我有一个已经用json_编码的数组,还有服务器上的文件,js,css,但我还不知道这个例子是如何工作的,这里是我的代码Php 关于autosuggest jquery插件的noob问题,php,json,autosuggest,Php,Json,Autosuggest,我想用这个插件为我的文本字段做一些自动提示 我有一个已经用json_编码的数组,还有服务器上的文件,js,css,但我还不知道这个例子是如何工作的,这里是我的代码 <html> <head> <title>test-data</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="i
<html>
<head>
<title>test-data</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="inc/css/admin_style.css" rel="stylesheet" type="text/css">
<link href="inc/css/autoSuggest.css" rel="stylesheet" type="text/css">
<script language="javascript" src="inc/js/functions.js"></script>
<script language="javascript" src="inc/js/jquery-1.5.1.js"></script>
<script language="javascript" src="inc/js/jquery.autoSuggest.js"></script>
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<center><br><font class="title">test</font></center>
<form action="dataAll.php" method="post">
Name: <input type="text" name="fname" />
<input type="submit" />
</form>
<p> </p>
<p>JSON</p>
<p> </p>
<?php
$encoded = json_encode ($familyNames);
echo $encoded;
?>
</body>
</html>
- 但问题是在哪里??(好像我把它放在php标记中,它给了我一个错误
- 我应该将json格式数组的名称放在哪里?$encoded,以便函数识别数据源
非常感谢!您将其放入html中的标记中
<script type="text/javascript">
$(function(){
$("input[type=text]").autoSuggest(data);
});
</script>
$(函数(){
$(“输入[类型=文本]”)。自动建议(数据);
});
你已经准备好了所有的部分,但是你的顺序/方法有点不对劲。试着创建第二个文件,命名为类似于ajax.php
,并将所有php代码放在其中。为了确保输出的是好的JSON,请添加行标题('Content-Type:text/JSON;charset=ISO-8859-1'))
在ajax.php
文件的最开始处(必须在发送任何输出之前设置标题,否则会出现错误)。现在您需要请求您的建议数据:
$(document).ready(function() { // Runs when your page is loaded in the user's browser
$.getJSON('ajax.php', function(data) { // Runs ajax.php, then executes an anonymous function to handle the response
$('input[name="fname"]').autoSuggest(data); // Set your input field to use automatic suggestions with the returned data
}); // end getJSON
}); // end ready
这段代码只是对ajax.php
执行一个异步HTTP请求,并将返回的JSON数据交给自动建议jQuery插件。将其放在
标记中。由于使用$(document),当页面加载时,它将运行一次。就绪(…)
。我添加了小优化(输入[name=“fname”]
),因此jQuery不会尝试将自动建议功能附加到页面上的每个文本输入。如果这是您想要做的(不太可能),只需将其更改回输入[type=text]
你真的不需要一个单独的php文件来完成这项工作。没有任何东西可以阻止你在一个文件中完成所有工作,但你很快就会意识到这会变得多么混乱和难以管理。对我来说,最容易将我的“ajaxy”php代码看作是我的web应用程序的一个单独的模块化部分
请务必参考以下页面了解详细信息:
$(document).ready(function() { // Runs when your page is loaded in the user's browser
$.getJSON('ajax.php', function(data) { // Runs ajax.php, then executes an anonymous function to handle the response
$('input[name="fname"]').autoSuggest(data); // Set your input field to use automatic suggestions with the returned data
}); // end getJSON
}); // end ready