1

I want to create jquery autocomplete and php, the problem is the data shown in each record of the autocomplete has two values, I managed to do this like this (without PHP),

$(function() {
        var authors = [
            {
                label: "jQuery",
                desc: "the write less, do more, JavaScript library"
            },
            {
                label: "jQuery UI",
                desc: "the official user interface library for jQuery"
            },
            {
                label: "Sizzle JS",
                desc: "a pure-JavaScript CSS selector engine"
            }
        ];

    $( "#authorname" ).autocomplete({
        minLength: 0,
        source: authors,
        focus: function( event, ui ) {
            $( "#authorname" ).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $( "#authorname" ).val( ui.item.label );
            return false;
        }
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
            .appendTo( ul );
    };
});

now I want to change the source to php page (change authors to "getauthors.php")

I don't know what to put inside "getauthors.php" in order to pass two things for label and desc.

I managed to pass one thing like this using json,

<?php
$json = array();
$json[]="test";
$json[]="test2";
$json[]="test3";

echo json_encode($json);
?>

which outputs like this,

autocomplete

you can see that the second thing is always undefined, how I can pass value to this in php using json (or any other way).

Thanks.

1 Answer 1

1
<?php
$json = array();
$json[] = array ("label"=>"test", "desc"=>"description");
...

echo json_encode($json);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked, I knew its stupid thing, I always put the second line the first array.

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.