的index.php
<div class="input-group">
<input type="text" class="form-control autosuggest" placeholder="Search for">
<span class="input-group-btn">
<button class="btn btn-info"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
<div class="dropdown">
<ul class="result">
</ul>
</div>
jquery / ajax文件
$(document).ready(function() {
$(".autosuggest").keyup(function() {
var search_term = $(this).attr('search');
var dataString = 'search_term='+ search_term;
$.ajax({
type: 'post',
url : 'search.php',
data: dataString,
success: function(data) {
alert(data);
}
});
});
});
search.php文件
<?php
include 'includes/db.php';
if(isset($_POST['search_term']) && !empty($_POST['search_term'])) {
$search_term = mysqli_real_escape_string($conn, $_POST['search_term']);
$sql = "SELECT name FROM names WHERE name LIKE $search_term%";
$run_sql = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_assoc($run_sql)) {
echo "<li>$rows[name]</li>";
}
}
?>
我只是在php mysql和ajax中制作一个简单的自动填充建议,我的php版本是5,jquery版本是2,我想在autosuggest容器中填充我的mysql数据,首先我试图提醒页面上的数据javascript警报功能的帮助,但是有些错误,任何人都可以查看我的代码
谢谢
答案 0 :(得分:0)
使用引号arround搜索变量
$sql = "SELECT name FROM names WHERE name LIKE '$search_term%' ";