这是我的代码
autocomplete.php
<html>
<head>
<script type="text/javascript"
src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript"
src="js/jquery.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#name").autocomplete({
source:'test.php',
minLength:1,
selectFirst: true
});
});
</script>
</head>
<body>
<form method="post" action="">
Name : <input type="text" id="name" name="name" />
</form>
</body>
<html>
test.php的
<?php
$my_string =$_GET['q'];
//echo $my_string;
syslog($priority, $my_string);
try {
// Connect to MongoDB
$conn = new Mongo('localhost');
// connect to test database
$db = $conn->mydb;
// a new products collection object
$collection = $db->QuoteCollections;
$quote_array = array(
'Author' => $my_string,
);
$cursor = $collection->find($quote_array);
//$json=$cursor;
foreach ($cursor as $obj)
{
//$json[]=array(
//'value'=> $student["Author"],
//'label'=>$student["Author"]
// );
echo 'Name: ' . $obj['Author'] . "\r\n<br>";
echo $row['Author']."\n";
}
//echo json_encode($json);
// close connection to MongoDB
$conn->close();
}
catch ( MongoConnectionException $e )
{
// if there was an error, we catch and display the problem here
echo $e->getMessage();
echo "error at mongoconexe";
}
catch ( MongoException $e )
{
echo $e->getMessage();
echo "error at mongoconeException";
}
?>
我无法得到结果,我引用了这些链接
http://phpseason.wordpress.com/2013/02/13/php-autocomplete-tutorial-using-jquery/
http://tutsforweb.blogspot.in/2012/05/auto-complete-text-box-with-php-jquery.html
我将两个js文件放在一个js文件夹中
请帮帮我
由于
答案 0 :(得分:0)
最后我明白了,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AutoComplete for Mongo with php</title>
</head>
<body>
<?php
try {
// Connect to MongoDB
$conn = new Mongo('localhost');
// connect to test database
$db = $conn->mydb;
// a new products collection object
$collection = $db->QuoteCollections;
// the array of product criteria
$query = $collection->find(array(),array("Author"=>1)); ?>
<label for="brgy">select:</label>
<input type="text" list="datlist" autocomplete="off" id="brgy">
<datalist id ="datlist">
<?php foreach($query as $result)
{
//echo $result['Author']; ?>
<option value="<?php echo $result['Author']; ?>"></option>
<?php
} ?>
</datalist>
<?php
// close connection to MongoDB
$conn->close();
}
catch ( MongoConnectionException $e )
{
// if there was an error, we catch and display the problem here
echo $e->getMessage();
echo "error at mongoconexe";
}
catch ( MongoException $e )
{
echo $e->getMessage();
echo "error at mongoconeException";
}
?>
</body>
</html>
在firefox中为我工作正常