我是全新的jquery。使用php和jquery進行實時搜索?
我有一個簡單的問題:我試過一個例子,使用PHP,MySQL和jQuery進行實時搜索,但它不顯示結果。
的html代碼:名爲 「getStates.php」
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type = "text/javascript">
function getStates(value){
$.post("getStates.php",{partialState:value}, function(data){
$("#results").html(data);
});
}
</script>
</head>
<body>
<input type = "text" onkeyup = "getStates(this.value)" />
<br>
<div id = "results"> </div>
</body>
</html>
我的PHP文件:
<?php
$con = mysql_connect("localhost", "root", "")
or die("Failed to connect to the server: " . mysql_error());
mysql_select_db("airlines")
or die("Failed to connect to the database: " . mysql_error());
$partialStates = strtoupper($_POST['partialState']);
$states = mysql_query("select source from flights where source like '%$partialStates%'");
while($row = mysql_fetch_array($states))
{
echo "<div>" . $row['source'] . "</div>";
}
?>
檢查錯誤,請檢查您的控制檯 –
**'mysql_ *'不推薦使用**。使用[PDO Prepared Statements!](http://php.net/manual/en/pdo.prepared-statements.php) – Ben
請[停止使用'mysql_ *'函數](http://stackoverflow.com/questions/ 12859942 /爲什麼-不應該-I-使用MySQL的函數式的PHP)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫](http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –