我正在尝试让自动建议搜索在 Wordpress 中从非 WP 表中提取数据。我正在使用“在帖子和页面中允许 PHP”插件。
我的搜索页面运行页面中定义的此代码
[php]
$themeDir = get_bloginfo('template_url');
echo "<html>";
echo "<head>";
echo "<script type='text/javascript' src='jquery/js/jquery-1.4.2.min.js'></script>";
echo "<script type='text/javascript' src='jquery/js/jquery-ui-1.8.2.custom.min.js'></script>";
echo "<script type='text/javascript'> jQuery(document).ready(function(){
$('#zipsearch').autocomplete({source:'http://127.0.0.1/wp/?page_id=230', minLength:2});
});
</script>";
echo "<link rel='stylesheet' href='jquery/css/smoothness/jquery-ui-1.8.2.custom.css' />";
echo "<style type='text/css'><!--
/* style the auto-complete response */
li.ui-menu-item { font-size:12px !important; }--></style>";
echo "</head>";
echo "<body>";
echo "<form onsubmit='return false;'>Enter a Zipcode:<input id='zipsearch' type='text' />";
echo "</form>";
echo "</body>";
echo "</html>";
[/php]
从中调用的结果页面 (page_id=230) 是
[php]
//$term = mysql_real_escape_string($_REQUEST['term']);
$term = $_REQUEST['term'];
$sql = "select zip, city, state from zipcode where zip like '$term%' order by zip asc limit 0,10";
$rs = mysql_query($sql);
$num = mysql_num_rows($rs);
$data = array();
if ($num > 0) {
for ($i=0;$i<$num;$i++) {
$zip = mysql_result($rs,$i,'zip');
$city = mysql_result($rs,$i,'city');
$state = mysql_result($rs,$i,'state');
$data[] = array('label' => $zip .', '. $city.' '. $state,'value' => $zip);
}
}
echo json_encode($data);
flush();
[/php]
结果页面似乎正在工作,因为如果我输入此 url (/wp/?page_id=230&term=331),我会得到以下结果
[{"label":"33101, Miami FL","value":"33101"},{"label":"33102, Miami FL","value":"33102"},{"label":"33107, Miami FL","value":"33107"},{"label":"33109, Miami Beach FL","value":"33109"},{"label":"33110, Miami FL","value":"33110"},{"label":"33111, Miami FL","value":"33111"},{"label":"33112, Miami FL","value":"33112"},{"label":"33114, Miami FL","value":"33114"},{"label":"33116, Miami FL","value":"33116"},{"label":"33119, Miami Beach FL","value":"33119"}]
问题是结果页面看不到从 jquery 正确调用,因为没有结果弹出到搜索窗口中。如果我检查元素,我可以看到结果页面的标记。
我在这里有一个截屏视频http://screencast.com/t/RH5NsErLf6BN,它显示了会发生什么。
希望我已经清楚了,如果没有,请告诉我在哪里可以澄清。