SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    0 Post(s)
    0 Thread(s)

    Autocomplete from a php array

    I have an array that has more than 500 elements.Each element contains a word with unicode character. I want to use the ajax's autocomplete feature.What should I do?

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    0 Post(s)
    0 Thread(s)
    Autocomplete isn't a "feature" of AJAX per se. AJAX is often used to fetch matches from the server in an implementation of autocomplete, but it isn't necessary if the array doesn't change while the page is loaded. In fact, you could simply JSON-encode the array in PHP and declare the array in JavaScript by echoing the encoded string in your <script> tagset when the page is rendered. Then your autocomplete widget could operate on the JavaScript array instead of using AJAX to query the server every time a key is pressed. This code hasn't been tested and requires some DHTML stuff to actually display the drop-down matches list, but it provides one piece of the puzzle.

    var choices = <php echo json_encode($myArray); ?>;
    function getMatches() {
     var matches = new Array();
     for (var i=0;i < choices.length;i++) if (choices[i].indexOf(document.getElementById('someForm').someTextInput.value) == 0) matches.push(document.getElementById('someForm').someTextInput.value);
     return matches;

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts