1

I have this ac.php file and this result.php file I want to use the autocomplete in JQuery 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); 

?>
4
  • try returning it as a jsonstring json_encode($arrResults). Also, have you checked on firbug to see whether the request/response is ok? Commented Apr 1, 2011 at 11:17
  • This is just the code you were given from here - are you asking how to integrate this with codeigniter specifically? Commented Apr 1, 2011 at 11:19
  • @Ross no, I can do it with codeigniter. The problem is when I used JQuery UI. its not retrieving the sample information Commented Apr 1, 2011 at 11:30
  • Hello, the error console of FireFox printed an error message. According of the documentation the method '.autocomplete()' needs the data in form of JSON. Commented Apr 1, 2011 at 11:37

2 Answers 2

3

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()`**][2] function will return the JSON string that corresponds to your array.
Sign up to request clarification or add additional context in comments.

1 Comment

I hope your still around :p .. I did exactly what you said and it worked. But I noticed something. Whatever i write the bar displays all words in the array. It doesnt comply what what the user enters.
1

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);
?>

2 Comments

I tried your way, but it didnt work :( + I dont understand the the second function, why use it?
The second function allows you to "autocomplete" on click and not only when you type something in the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.