在我的网站上,我收到了这个Ajax电话:
//Autocomplete
$(document).ready(function() {
// Autocomplete for names
$("#search-input-name").keyup(function(){
$.ajax({
type: "POST",
url: "function/autocomplete.php",
data: {action : "name", keyword: $(this).val()},
success: function(data){
// Actions
}
});
});
// Autocomplete for cities
$("#search-input-city").keyup(function(){
$.ajax({
type: "POST",
url: "function/autocomplete.php",
data: { action: "city", keyword: $(this).val()},
success: function(data){
// Actions
},
});
});
});
此脚本调用此php文件:
<?php
include('db_connect.php');
if (!isset($_POST['action']) && !isset($_POST['keyword'])) {
echo 'Error';
die();
} else {
switch ($_POST['action']):
case 'name':
$sql = "SELECT name FROM profiles WHERE name LIKE '" . $_POST['keyword'] . "' ORDER BY name LIMIT 1";
$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
$resultset[] = $row;
}
if (!empty($resultset)):
?>
<ul id="name-list">
<?php foreach($resultset as $name):?>
<li onClick="selectName('<?php echo $name["name"]; ?>');"><?php echo $name["name"]; ?></li>
<?php endforeach;?>
</ul>
<?php
endif;
break;
case 'city':
$sql = "SELECT city FROM profiles WHERE city LIKE '" . $_POST['keyword'] . "' ORDER BY city LIMIT 1";
$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
$resultset[] = $row;
}
if (!empty($resultset)):
?>
<ul id="city-list">
<?php foreach($resultset as $city):?>
<li onClick="selectCity('<?php echo $city["city"]; ?>');"><?php echo $city["city"]; ?></li>
<?php endforeach;?>
</ul>
<?php
endif;
break;
endswitch;
}
脚本运行正常但有时我遇到403错误。 我不知道是否必须更改服务器端的某些配置。
感谢所有帮助