0

I'm trying to use jquery autocomplete for a text input. What I want is that when user types a letter, results starting with the specified letter will be shown from an xml source. But I can't make it work. As I'm quite new in jquery, I don't know what I'm doing wrong. Please help :)

Also I tested the php file, it works fine as xml

Here's the code

$("#names").autocomplete({
    source: function(request , response){
        $.ajax({
           type: 'GET',
            url: 'name.php',
            dataType: "xml",
            data: "letter="+request,
            success: function(data) {
                var xml;

                if (typeof data == "string") {
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(data);
                } else {
                    xml = data;
                }

                var array = [];
                var i = 0;

                $(xml).find('nameslist').each(function(){
                    array[i] = $(this).find("name").text();
                    i++;
                });
            }
        }); 
        response(array);
    },
    minLength: 1
});     

4 Answers 4

1

So go with JSON :)

$("#names").autocomplete({
    source: function(request , response){
        $.ajax({
           type: 'GET',
            url: 'name.php',
            dataType: "json",


In your 'name.php' do following

<?php
  // do all your code here, get names etc....

  // lets say your array with names looks like this
  // $names = array('Peter', 'John', 'Tom', 'Andy');

  // serialize the array and send it to the browser
  echo json_encode($names);     // edited here
?>


In your .js file...

  var names = jQuery.parseJSON('["Peter","John","Tom","Andy"]');
  // just to try, if this works, uncoment alert() below this
  //alert( names[3] );


So my complete code would be like this

$("#names").autocomplete({
    source: function(request , response){
        $.ajax({
            type: 'GET',
            url: 'name.php',
            dataType: "json",
            data: "letter="+request,
            success: function(data) {
                var names = jQuery.parseJSON(data);   
            }
        }); 
        response(names);
    },
    minLength: 1
});
Sign up to request clarification or add additional context in comments.

6 Comments

well if I use serialize function in php I get exception not caught error and if I don't then names returns null. Also I think I'll have a charset problem with json_encode, my results will be in iso-8859-9 charset
I am sorry, just skip "serialize", JSON encode is enough for this operation. My fault, I edited the code ;-)
What is the status your progress? Do you need any other help?
actually I equalized success to response, then it worked fine but then I had the charset problem as I expected, so now I'm using a plugin from bassistance.de
I'm using jquery UI autocomplete 1.8 back again and I found a very simple solution. I put "|" between each result in php and sending a single string to js, then in js I split the data by "|" and assign it to an array and it works :) thank you very much for your effort
|
1

Here's the solution I found for the code above

in .js

$("#names").autocomplete({
    source: function(request , response){
        $.ajax({
            type: 'GET',
            url: 'names.php',
            data: "letter="+$("#names").val(), //request doesn't work here, I don't know why
            success: function(data) {
                var explode = data.split("|");                      
                response(explode);
            }
        }); 
    }
});

in php file

if(isset($_GET['letter'])){
    $letter = $_GET['letter'];
    $sql = "select name from name_list where name like '".$letter."%'";
    $query = mysql_query($sql);

    while($result = mysql_fetch_row($query)){
        echo $result[0].'|';    
    }
}

Comments

0

I'm new in JQuery myself, but shouldn't it be typeof(data) == "string" ?

2 Comments

generally ajax function works fine, I used same syntax in several functions and it didn't make a problem
Do you know exactly where it fails? Have you dropped in ALERT statements to step through the code?
0

Why would you want to grab a letter from an xml file? Why not attach the letters using .html to a div below. If your doing an autocomplete id have a mysql database which would hold a list of items for autocomplete and for the textbox every keyup check if the textbox has a string inside if it does make an ajax request with jquery to your php file where you then strip the $get searchword then you can have it inside the php file grab results like the string and have javascript at the bottom attach the string to your div wwith html

2 Comments

I can post some code later when I get home from school, if you need any more explanation or help feel free to ask
actually it would be nice if you could post some examples on how the formats of the divs should be and yes I'm using mysql to get results

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.