Similar Question:
Sending PHP json_encode array to jQuery
I've created a function that searches a database for a specific name using $.post. It returns user details with matching search criteria in JSON format generated by PHP, like this:
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] ) )
The JavaScript then retrieves the user input from the HTML and sends it to the PHP script.
function searchuser() {
var searchvar = $('#searchbar').html();
$.post("Scripts/search.php", {name: searchvar}, function(data) {
alert("userid: " + data[0][user]);
});
}
I'm attempting to display the number 17 in an alert, but I'm encountering issues. It seems like there might be a syntax error, but I'm unable to pinpoint the correct approach. Despite using jQuery, I wanted to try plain JavaScript first. Appreciate any help you can provide!