Results 1 to 2 of 2

Thread: Problem with jQuery & AJAX GET

  1. #1
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Problem with jQuery & AJAX GET

    Hi,

    I'm using jquery to make an Ajax GET call. Basically on my page I have 3 buttons to add products. What I want is for all 3 to be added to my shopping cart page at once. It works fine when I don't have a redirect and go to the cart page manually. The problem is when I add the redirect code, it only adds the first product. Any help on this problem would be much appreciated.

    Code:
    var dlink1 = $('a.addlink1').attr('href'); 
    var dlink2 = $('a.addlink2').attr('href'); 
    var dlink3 = $('a.addlink2').attr('href'); 
    
    
    $('a.submitbutton').click(function() {
    
        $.ajax({
                type: 'GET',
                beforeSend: function() {
    
    if(dlink1 != '') { $.get(dlink1) }
    if(dlink2 != '') { $.get(dlink2) }
    if(dlink3 != '') { $.get(dlink3) }
    
                },
               success: function() {
    window.location = '/cart.php';      
          }
            });
    
    
        return false;
    });

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Putting 3 gets inside another get makes little sense. Especially since the outer get isn't getting anything. I'm surprised it does anything at all. Are you sure there's not other code on the page that's causing it to change to the cart page? Or it might just be that the code is erroring out, so the form submits normally. But then again, the way you have the code there, it looks like the click event is being assigned to an a tag. So maybe it's firing normally as a link. Hard to tell without seeing the whole page.

    Anyways, make sure you don't have other code that's loading up the cart page and get rid of it if it's there and try this (replaces all of the code in your post):

    Code:
    $('a.submitbutton').click(function() {
    	var urls = 0;
    
    	var dlink1 = $('a.addlink1').attr('href'); 
    	var dlink2 = $('a.addlink2').attr('href'); 
    	var dlink3 = $('a.addlink2').attr('href');
    
    	if(dlink1 != '') { ++urls; }
    	if(dlink2 != '') { ++urls; }
    	if(dlink3 != '') { ++urls; }
    
    	function success(){
    		if(--urls == 0){window.location = '/cart.php';}
    	}
    
    	if(dlink1 != '') { $.get(dlink1, success); }
    	if(dlink2 != '') { $.get(dlink2, success); }
    	if(dlink3 != '') { $.get(dlink3, success); }
    
    
    	return false;
    });
    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 02:21 PM
  2. jQuery ajax help
    By keyboard in forum JavaScript
    Replies: 3
    Last Post: 04-14-2012, 02:28 PM
  3. jquery and ajax form problem
    By saad_sinpk in forum Other
    Replies: 10
    Last Post: 10-27-2011, 08:12 PM
  4. Resolved jQuery $.ajax fails in IE.
    By JShor in forum JavaScript
    Replies: 2
    Last Post: 08-01-2011, 09:14 PM
  5. [?] run JS in AJAX-jQuery
    By temp304 in forum JavaScript
    Replies: 3
    Last Post: 03-07-2008, 06:17 PM

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
  •