Hi all, I have an online store, I know understand PHP very well. Starting to pick up the basics with jQuery but a little stuck on how when I do an AJAX request, how I can return multiple values to be displayed in the page. For example, when the user clicks the ‘Quick Add’ button, I want to change the number of items at the top of the page in the users cart and the total price of everything in the cart.
How can this be done? Any help or advice is appreciated
Set ‘dataType’: ‘json’ when requesting data from server, in your php code generate list of variables for your reply, put it in array and use echo json_encode($result); to echo it. Then in ajax’s success event data will contain that array. Parse it to change content.
Something like this:
$.ajax({‘url’: ‘/script.php’, ‘dataType’: ‘json’, ‘success’: function(data) {
if(typeof(data.itemsTop) != ‘undefined’) $(‘#items-top’).text(data.itemsTop);
etc…
}});
and in php script:
$result = array(
‘itemsTop’ => 5,
and the rest of variables here…
);
echo json_encode($result);
exit;
Hey dude, thanks so much for your message, that’s a massive help. Really easy to understand. I’ll have a play and see how I get on, cheers again dude!