我有一个搜索框,我将在其中搜索名称,如果该名称存在于数据库中,则应获取该名称并将其显示在选择框中。我无法做到这一点。
<input type="text" class="form-control searchbox" name="search" id="searchbox">
<div id="re"></div>
$(document).on("keyup", ".searchbyname", function() {
var searchname = $(this).val();
console.log(searchname);
if (searchname != '') {
$.ajax({
type: "POST",
url: "search.php",
data: {
searchname: searchname
},
success: function(html) {
$('#re').html(html);
}
});
}
return false;
});
$name = $_POST['searchname'];
$sql_res = $conn->query("select *from tbl where firstName like '".$name."%'");
if ($sql_res->num_rows > 0)
{
while ($row = $sql_res->fetch_assoc()) {
$row['name'];//here how to send back name in select box i am not able to do so
}
}
答案 0 :(得分:1)
您需要使用echo
将数据发送回php。在您的php代码中,如下所示:
if($sql_res->num_rows > 0)
{
echo " <select name='na'>";
echo "<option value=''>Select</option>";
//if names exists print the value in option
while($row=$sql_res->fetch_assoc())
{
echo "<option value='". $row['id']."'>".$row['name']."</option>"; //here printing name
}
echo "</select>";
}