Hesham Saeed
Hesham Saeed

Reputation: 5378

jquery autocomplete with php source & json issue

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.

Upvotes: 1

Views: 982

Answers (1)

phantasm
phantasm

Reputation: 572

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

echo json_encode($json);
?>

Upvotes: 1

Related Questions