我正在尝试使用 jquery 自动完成插件,但我没有成功!我已经阅读了很多示例,我的代码似乎是正确的,但不起作用!
我的 HTML 代码是:
<?php require_once 'header.php';?>
<html>
<head>
<title></title>
<script>
$("#country").autocomplete({
source: "country_autocomplete.php",
autoFocus: true
});
</script>
</head>
<body>
<form action="" method="post" >
<ul>
<li>
<label> E-mail:</label><input class="fr" type="text" name="uemail" size="60%"/>
</li>
<li>
<label> Phone:</label><input class="fr" type="text" name="uphonenum" size="60%"/>
</li>
<li>
<label> Firstname:</label><input class="fr" type="text" name="ufname" size="60%"/>
</li>
<li>
<label> Last name:</label><input class="fr" type="text" name="ulastname" size="60%"/>
</li>
<li>
<label> Country:</label><input class="fr" type="text" name="ucountry" id="country" size="60%"/>
</li>
<li>
<label> State or Province:</label><input class="fr" type="text" name="state" id="ustate" size="60%"/>
</li>
<li>
<label> City:</label><input class="fr" type="text" name="ucity" id="city" size="60%"/>
</li>
<li>
<label> Post-code:</label><input class="fr" type="text" name="upostcode" id="postcode" size="60%"/>
</li>
<li>
<label> Adress 1*:</label><input class="fr" type="text" name="uadrr1" size="60%"/>
</li>
<li>
<label> Adress 2:</label><input class="fr" type="text" name="uadrr2" size="60%"/>
</li>
<li>
<label> Compagny name:</label><input class="fr" type="text" name="ucompanyname" size="60%"/>
</li>
<li style="background-color: #000;">
<input class="fr" type="submit" name="submitpwd" style="width: auto;" value="Update"/>
</li>
</ul>
</form>
</td>
</tr>
</table>
<?php require_once 'footer.php';?>
</body>
</html>
我的 country_autocomplete.php 文件是:
<?php
define('DB_SERVER', 'localhost');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pswd');
define('DB_NAME', 'db_name');
if (isset($_GET['term'])){
try {
$conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$stmt = $conn->prepare('SELECT country.name FROM country WHERE country.name LIKE :term');
$stmt->execute(array('term' => $_GET['term'].'%'));
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
echo json_encode($row);
}
} else {
echo "No rows returned.";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
?>
有人可以告诉我有什么问题吗??