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 file
$(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 file
<?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>";
}
}
?>
I am just making a simple autocomple suggestion in php mysql and ajax, my php version is 5 and jquery version is 2,i want to populate my mysql data in autosuggest container,First i am trying to alert the data on page with the help of javascript alert function but thrie is some error,can anyone please review my code
Thank you
使用引号arround搜索变量
$sql = "SELECT name FROM names WHERE name LIKE '$search_term%' ";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.