JQuery ui autocomplete with php (codeigniter) and database
I have this ac.php file and this result.php file I want to use the autocomplete in JQue开发者_如何学Gory UI in the ac.php file, but I want it to retrieve the data from result.php as a source. I just dont know how to do it. I tried this way, but its not working
ac.php
<html>
<head>
<link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#tags").autocomplete("result.php");
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
this is result.php
<?php
$arrResults = array('option 1', 'option 2', 'option 3');
// Print them out, one per line
echo implode("\n", $arrResults);
?>
Quoting the documentation of Autocomplete :
the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.
So, instead of your code, you should use something like this :
<?php
$arrResults = array('option 1', 'option 2', 'option 3');
echo json_encode($arrResults);
?>
The json_encode()
function will return the JSON string that corresponds to your array.
ac.php
$(function() {
$( "#tags" ).autocomplete({
source: "result.php",
dataType:'json',
minLength: 0,
delay:0
});
$( "#tags" ).click(function() {
$( "#tags" ).autocomplete("search","");
});
});
result.php
<?php
$arrResults = array('option 1', 'option 2', 'option 3');
// Print them out, one per line
echo json_encode($arrResults);
?>
精彩评论