我似乎有一个问题。我有一个输入字段和一个<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);
}
?>
答案 0 :(得分:0)
您可以按照以下要求尝试使用ajax,
$.ajax({
type:"POST",
dataType:"json",
url: 'search.php',
data: {order_numbers: values, order_state: status},
success: function(response){ },
error: function(response){}
});