typehead建议在真正的主机中不起作用,但是在localhost中它可以正常工作,这是它在真正的主机中不起作用的原因。
这是我的代码
html代码
<form method="POST" action="result.php">
<input type="text" name="city" class="city form-control" Placeholder="Enter Your Area!">
<input type="submit" name="submit" role="button" class="btn btn-primary btn-lg" style="margin-top:-11px;background-color:#FFA500;">
</form>
javascript代码
<script src="js/typeahead.min.js"></script>
<script src="js/bootstrap-3.1.1.min.js"></script>
<script>
$(document).ready(function() {
$('input.city').typeahead({
name: 'city',
remote: 'search.php?query=%QUERY'
});
})
</script>
PHP代码(search.php)
<?php
//CREDENTIALS FOR DB
define ('DBSERVER', 'localhost');
define ('DBUSER', 'root');
define ('DBPASS','');
define ('DBNAME','hotel');
//LET'S INITIATE CONNECT TO DB
$connection = mysql_connect(DBSERVER, DBUSER, DBPASS) or die("Can't connect to server. Please check credentials and try again");
$result = mysql_select_db(DBNAME) or die("Can't select database. Please check DB name and try again");
//CREATE QUERY TO DB AND PUT RECEIVED DATA INTO ASSOCIATIVE ARRAY
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$sql = mysql_query ("SELECT locations, loc_id FROM delhi WHERE locations LIKE '%{$query}%' OR loc_id LIKE '%{$query}%'");
$array = array();
while ($row = mysql_fetch_array($sql)) {
$array[] = array (
'label' => $row['locations'].', '.$row['loc_id'],
'value' => $row['locations'],
);
}
//RETURN JSON ARRAY
echo json_encode ($array);
}
?>