1

I'm using this jquery plugin : JQuery Autocomplete. Problem I'm getting json data but it's not appearing on the autocomplete list. The JQuery code:

$( "#student-id" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "ajax/ajax_admin.php?auto_student=" + $( "#student-id" ).val(),
            dataType:"json",
            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.students, function( item ) {
                    return {
                        label: item.id +" , "+ item.name,
                        value: item.id
                    }
                }));
            }
        });
    },
    minLength: 2,
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
});

The PHP Script is :

public function load_ajax_student_list($val)
{
    $val = "%".$val."%";
    $stmt = $this->conn->prepare("select * from student where studentAiubId like :value limit 0,5");
    $stmt->bindValue(':value', $val);
    $stmt->execute();
    if($stmt->rowCount() < 1)
        echo "";
    else
    {
        $result = $stmt->fetchAll();

        $output = array();
        foreach($result as $row)
        {
            if($row['mname']=="")
                $name = $row['fname']." ".$row['lname'];
            else
                $name = $row['fname']." ".$row['mname']." ".$row['lname'];
            $data["name"] = $name;
            $data["id"] = $row['studentAiubId'];
            $output["students"][] = $data;
        }
        echo json_encode($output);                
    }
}

If the call goes like this: ajax/ajax_admin.php?auto_student=10
The produced data from PHP script is :

{
    "students": [
        {"name":"Moh Mamun Sardar","id":"10-15987-1"},
        {"name":"Rehan Ahmed","id":"10-12451-2"},
        {"name":"Abid Hassan","id":"10-15412-1"},
        {"name":"Abir Islam","id":"10-11245-1"}
    ]
}

But Nothing showing on autocomplete. What I'm doing wrong?

3
  • 1
    item.value must be item.name or item.id.There is no field called "value on returning json" Commented Jan 14, 2014 at 11:23
  • i'was just using that before i posted this. sorry my bad. editing Commented Jan 14, 2014 at 11:25
  • thanks anyway. I was also using that. Didn't work for me. Now all of a sudden it's working. :) Commented Jan 14, 2014 at 11:34

2 Answers 2

3

try something like this

$.map( data.students, function(item ) {
    return {
    label: item.name,
    value: item.id
});

it is minlength not minLength see casing

Sign up to request clarification or add additional context in comments.

3 Comments

But Here in view source on their website : jqueryui.com/autocomplete/#remote-jsonp -It's minLength
you can here in this fiddle by changing casing see whether it working or not jsfiddle.net/8khSs/1
@MamunSardar might be some problem in fiddle.net
0

You have forgotten the "appendTo" property. In this propery you have to specify the selector of the element you wish the information to be appended to like this

appendTo: '.class' or appendTo: '#id'

You have to add this property to the initialization of the autocomplete as a sibling of source and etc ...

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.