0
我正在做一個自動完成功能使用PHP的jQuery.On在警報框中輸入第一個字母它正在顯示values.I需要得到的值爲dropdownlist。如何將Php Json值作爲下拉與Jquery?
我的編碼是:
<html>
<head>
<script>
$(document).ready(function(){
$("#localty").keyup(function(){
var localty = $("#localty").val();
$.ajax({
type : "POST",
url: "getlist.php",
dataType : "json",
data : "localty="+localty,
success: function(result){
alert(result);
}
});
});
});
</script>
</head>
<body>
<input type="text" id="localty">
<input type="submit" name="submit" value="Submit">
</body>
</html>
getlist.php頁:
<?php
$localty = $_REQUEST['localty'];
$con = mysqli_connect('localhost','root','','details');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"details");
?>
<?php
$sql="SELECT localty FROM localty WHERE localty like '$localty%'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$row_set[] = $row['localty'];
}
echo json_encode($row_set);
mysqli_close($con);
?>
請給出任何建議。