1

The jQuery:

$("[type=text]").autocomplete({
source: "json.php",
minLength: 0
}).addClass("ui-widget ui-widget-content ui-corner-left");

Works fine if I change the source: to a JS array. I know that the php is working because I can see it in the console, but here is the sample php anyways:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);

So the dropdown just isn't displaying. Feeling pretty stupid right about now.

3
  • The JSON from your PHP is being encoded to a object, not an array. I'm not intimately familiar with jQuery.autocomplete, but is it expecting an array instead? {'a': 1, 'b': 2, 'c': 3} vs ['a', 'b', 'c'] Commented May 3, 2011 at 18:15
  • Even when i try to return: echo "['opt1','opt2','opt3','opt4']"; from the php page, it doesn't do anything. I've also tried adding dataType: "json", to the options and nothing. Commented May 3, 2011 at 18:43
  • 1
    the problem is that you need 'label' or 'value' property in your json object to make the render automatically otherwise you need a custom select function Commented May 3, 2011 at 21:02

3 Answers 3

2

In your json.php file, be sure to set the content encoding to be json via the header() function before your echo. This way jQuery should see the array as proper json.

header("Content-Type: application/json");
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
Sign up to request clarification or add additional context in comments.

1 Comment

I added it, but still nothing.
1

A few days ago I was also having problems with a JSON-populated Autocomplete.

My problem was that I wasn't setting the content-type, as Wally said above:

header("Content-Type: application/json");

I also wrapped my jQuery autocomplete call inside a getJSON, then used the data from that function to populate the autocomplete field. My gut tells me that the extra getJSON shouldn't be necessary, but like you, I was having problems referencing my PHP file as the source.

$.getJSON("json.php", function(data) {
    $("[type=text]").autocomplete({
        dataType: "json",
        source: data,
        minLength: 1,
    }); 
});

Since I'm using an old PHP version, I hand-made my JSON. I included "label" and "value" fields because I'm fairly certain they're necessary for the autocomplete to work.

$jsonList = "[{"label" : "Item 1", "value" : "1"}, {"label" : "Item 2", "value" : "2"}]";
return $jsonList;

Comments

1

http://jqueryui.com/demos/autocomplete/remote-jsonp.html

Check this get from demos site.

$( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });

7 Comments

I don't think so, I'm getting the response text in the console.
Yes, but you don't execute this code in your console ? What is the site structure ? /subpage/my_form.php and /subpage/json.php, can you get the json.php like that ? Check your javascript error in your browser
Was talking about the browser's console window (actually firebug). Anyways json.php is in the same dir and I tried what you had suggested above - still nothing. I see responsetext but nothing happens on the page.
yeah, i saw that but I have no idea what that back end looks like, I figured I'll need to parse the json some how, I just have no clue.
@jreed121 This is the backend at ws.geonames.org/searchJSON :{"totalResultsCount":0,"geonames":[]}
|

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.