0

Possible Duplicate:
Sending PHP json_encode array to jQuery

I have a little function that searches a database for a name through $.post, and it returns details of users with matching search details. The json is produced in php and looks like so:

Array (
[0] => Array ( [user] => 17 [fn] => blah [ln] => gnaaa [email] => [email protected] )
[1] => Array ( [user] => 18 [fn] => blee [ln] => gnaaa [email] => [email protected] )
[2] => Array ( [user] => 19 [fn] => orange [ln] => gnaaa [email] => [email protected] ) )

Now the javascript pulls the user input data from the html, and posts it to the php script

function searchuser() {  
    var searchvar = $('#searchbar').html();
    $.post("Scripts/search.php", {name: searchvar}, function(data) {
        alert("userid: " + data[0][user]);
    });
}  

So here I am trying to alert the number 17, but nothing seems to be working. I'm guessing I've got something wrong with the syntax, but I can't figure out what the right way is. I am also using jQuery, so I'm guessing there's a neater way to do this but I wanted to try plain javascript first. Thanks in advance

1
  • There are other ways as well though: - Set the right content response headers in your PHP script. - Pass 'json' as fourth parameter to $.post. Commented Jan 23, 2013 at 15:26

1 Answer 1

0

In order to have 'data' parsed as a JSON object, you will need to add dataType: "json" or "jsonp" to your post call.

Reference: http://api.jquery.com/jQuery.ajax/

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

1 Comment

This and I needed single marks around user so ['user'] Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.