0

I'm using php to fetch the data from mysql database and then use that data on the jquery ui autocomplete. But it seems like the JSON that is returned doesn't match what jquery ui needs. The JSON returned looks something like this:

["dyes miran famint","annie ferrer mendrez","annie ferrer mendrez","anton setra masgre","anton setra masgre","jeniffer hades cruz","jeniffer hades cruz","alvin louie urbano maranon","alvin louie urbano maranon","francisza jerrielleza bullonza","blaze br tags anchor"]

I even used jQuery.parseJSON on it.

And here's what I did on the php file:

$class_code = $_POST['class_code'];

$student_list = array();
$students = mysql_query("SELECT accounts.id, fname, mname, lname, classcode
                        FROM account_details
                        LEFT JOIN accounts ON account_details.id = accounts.id
                        LEFT JOIN class_rooster ON accounts.id = class_rooster.id
                        WHERE accounts.status = 1 AND accounts.type='student'
                        AND (class_rooster.id  IS NULL OR classcode !='$class_code')");
$students_num = mysql_num_rows($students);
if($students_num > 0){
    while($row = mysql_fetch_assoc($students)){

        $student_list[] = $row['fname'] . ' ' . $row['mname'] . ' ' . $row['lname'];

    }
    echo json_encode($student_list);

Here's the error that I'm getting:

Uncaught TypeError: Property 'source' of object #<Object> is not a function

Here's the JavaScript:

function get_students(){
        var class_code = $('#current_classes').val();
        $.post('student_list.php', {'class_code' : class_code},
            function(data){

                return data;
            }
        );
    }


    $("#student_name").live('click', function(){

        $("#student_name").autocomplete({
            source:  get_students()
        });
    });
4
  • I might be mistaken but I think it expects an array of objects with id and value properties, not just an array with strings. Commented Mar 14, 2012 at 16:09
  • 1
    @pinkypower - An array of strings will work according to the docs: jqueryui.com/demos/autocomplete/#remote The PHP and generated JSON look fine, the problem is likely somewhere in your Javascript. Could you post that? Commented Mar 14, 2012 at 16:11
  • yep, you're right, just checked it myself :) Commented Mar 14, 2012 at 16:12
  • please see my edit for the JavaScript code Commented Mar 14, 2012 at 16:21

3 Answers 3

1

Check out the Overview section of the documentation on how to use the callback source type. The second argument passed to the function is the response callback to be used. Your get_students() function should look something like this:

function get_students(current_val,callback){
        var class_code = $('#current_classes').val();
        $.post('student_list.php', {'class_code' : class_code},
            function(data){
               callback(data);
            }
        );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the documentation, it's looking for something like this:

$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "Scheme"
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});

My guess is that you are doing $().autocomplete(json_obj) instead of $().autocomplete({source: json_obj}).

Comments

0

I think you are not using the source entity in the autcompelete plugin code. Check your auto complete code

$("#inputbox").autocomplete({ source: data });

I believe You may use data without source entity

Also check your php output array

Comments

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.