我一直在使用google jquery API在PHP中进行即时搜索。 在youtube视频中,该程序正在运行(因为我正在从该视频中学习)。 我的数据库逻辑是正确的,因为我在简单的html页面中尝试过它。 但它不适用于jquery。请给我一些建议来解决这个问题。 我正在研究ubuntu 14.04。
这是我的jquery包含的PHP页面。
<!DOCTYPE html>
<html>
<head>
<title>WebPage</title>
<script type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type=text/javascript>
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post('search.php', {search_term: searchTxt}, function(output){
$('#output').html(output);
});
}
</script>
</head>
<body>
<form action="index.php" method="post">
<b> First Name: </b> <br/>
<input type="text" name="search" id="search1" placeholder="Search for members ..." onkeydown="searchq();">
<br/> <br/> <b> Submit: </b>
<input type="submit" name="Submit">
</form>
<div id="output"></div>
</body>
我的日期连接页面如下
<!DOCTYPE html>
<html>
<head>
<title>WebPage</title>
</head>
<body>
<?php
$connection = mysqli_connect("localhost", "root", "password");
$output = "Search Results. <br/> <br/>";
if(isset($_POST["search_term"])){
$name = $_POST["search_term"];
$sql = "SELECT cust_id, last_name, first_name, email FROM widget_corp.customer WHERE first_name LIKE '%"
. $name . "%' OR last_name LIKE '%" . $name . "%'";
$result = mysqli_query($connection, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
echo "There were no search results.";
}
else {
while($row = mysqli_fetch_assoc($result)){
$firstName = $row["first_name"] . " ";
$lastName = $row["last_name"] . " ";
$Email = $row["email"] ;
$output .= '<div>'.$firstName.' '.$lastName.'</div>';
}
}
}
echo ($output);
?>
</body>