[英]Dropdown list not populating from database
我似乎有一个问题。 我有一个输入字段和一个<select>
字段。 我需要在输入字段中键入一个位置,如果该单词与数据库中的记录匹配,则它应该是我的<select>
下拉列表中人员的名字。 这是我的index.php文件:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<input type="text" name="location_input" id="location_input">
Tutor:<select name="locations" id="locations">
<script>
$("#location_input").keyup(function(){
const location = $("#location_input").val();
$("#locations").html(''); //reset dropdown
// do ajax call to get locations
$.ajax({
url: 'search.php', //replace this with your route of the search function
data: {location}, //pass location as body data
dataType: 'json', //expect a json response back
success: function(data) {
data.forEach(function(el) { //loop over the json response
let option = `<option id=${el.id} value=${el.name}>${el.name}</option>`
$("#locations").append(option); //append locations to select dropdown
});
},
error: function(err) { //error functions
console.log(err);
alert("Error")
}
});
});
</script>
</select>
</body>
</html>
这是我的search.php文件:
<?php
function SearchLocations() {
$conn = new mysqli('localhost', 'root', '', 'tutors') or die ('Cannot connect to db');
$result = $conn->query("select * from tutor_location where Location_tags LIKE ='%". $_GET['location']."%'");
$locations = [];
while ($row = $result->fetch_assoc()) {
$locations[] = $row;
}
return json_encode($locations);
}
?>
这是我的数据库的屏幕截图: 我面临的问题是,它给了我一个localhost says error
,而控制台没有向我显示错误。
您可以尝试以下要求的ajax,
$.ajax({ type:"POST", dataType:"json", url: 'search.php', data: {order_numbers: values, order_state: status}, success: function(response){ }, error: function(response){} });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.