【发布时间】:2014-10-27 17:07:51
【问题描述】:
我正在使用 AJAX 和 jQuery 创建一个自动完成文本框。这是我的客户端 AJAX 代码。
$(document).ready(function(){
$('#tagtext').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'country'
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item,
value: item
}
}));
}
});
},
autoFocus: true,
minLength: 0
});
});
这是我的服务器端ajax.php 代码:
<?php
require_once 'db_const.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(isset($_GET['type']) AND $_GET['type'] == 'country'){
$result = $mysqli->query("SELECT tagname FROM tagtable where tagname LIKE '".strtoupper($_GET['name_startsWith'])."%'");
$data = array();
while ($row = mysqli_fetch_array($result)) {
array_push($data, $row['tagname']);
//echo "Hello " .$row['tagname'];
}
echo json_encode($data);
} else {
echo "hello";
}
?>
错误是我在$_GET['type'] 中没有获得 country 的值(即 $_GET 未设置)并且它超出了 if 条件 并执行else 分支。
谁能帮助我了解如何获得"$_GET['type'] == country"?
【问题讨论】:
标签: php jquery ajax autocomplete