我正在尝试使用 PHP 和 jQuery 做一个自动建议文本框,但是,我使用的所有基于互联网的 jQuery 已经被弃用。我不确定我的代码是不工作的还是 jQuery 的。有人可以帮我吗?:) 先感谢您!
<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>jQuery Autocomplete Plugin</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
<script type="text/javascript">
$().ready(function() {
$("#users").autocomplete("autoCompleteMain.php", {
width: 260,
matchContains: true,
selectFirst: false
});
});
</script>
</head>
<body>
<h2 id="banner">Autocomplete</h1>
<div id="content">
<form autocomplete="off">
<p>
Enter Username <label>:</label>
<input type="text" name="users" id="users" />
</p>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
PHP:
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="aaaa"; // Mysql password
$db_name="maptemp"; // Database name
$con = mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select username from users where username LIKE '%$q%'";
$rsd = mysql_query($sql);
while($row = mysql_fetch_array($rsd)) {
$cname[] = $row['username'];
}
echo json_encode($cname);