SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery Shopping Cart AJAX

    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

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •