1

I'm sure I'm overlooking something here... I have some jQuery code that fires off when a form button is pressed. The button is pressed, a coupon code is looked up and written in a div on the form and then the discount is applied (total = price - discount). Problem is, when I step thru Firebug to debug it, about 80% of the time the code works. However, when I run the code, it doesn't work. It's like the code runs too fast for the variables to get the correct information. Here's the code:

$('#coupon-submit').click(function() {
    applyCoupon($('#coupon'), $(this));

    var price = $('#price-total').text();
    price = price.substring(5,price.length);

    var theText = $('#fulldiscounttext').text();  // Sometimes this has a value while debugging, sometimes not
    var discount = theText.substring(1, theText.length-4); // Takes unwanted characters out of string

    var total = price - discount;

    $('#price-total').text('US $ ' + total.toFixed(2));
    var thetotal = $('#price-total').text();
    $('#grandtotal').val(thetotal);
});

applyCoupon() looks up the code and writes it into the div #fulldiscounttext. I'm trying to have the #price-total div update with the discounted amount. It's not updating (unless I'm debugging it).

The applyCoupon() function:

function applyCoupon(couponInput, couponButton)
{
    removeInputNames();
    $.ajax({
        data: $('#orderform').serialize(),
        type: 'POST',
        timeout: 5000,
        url: 'coupon.php',
        dataType: 'text',

        beforeSend: function(request)
        {
            $('#coupon-error').text('');
            couponButton.attr('disabled', 'disabled');

        },
        success: function(data, textStatus)
        {
            couponButton.removeAttr('disabled');

            if(data == "bad error code")
                couponOK = 0;
            else
                couponOK = 1;

            if (!couponOK)
            {
                var badcode = couponInput.val().toString();
                if (badcode.length > 0)
                {
                    var fmt = 'You\'ve entered an invalid code.';
                    $('#coupon-error').text(fmt);
                }
            }
            else    // Coupon recognized!
            {
                $('#total-row').before('<tr><td colspan="4">'
                    + '<div id="fulldiscounttext">' + data
                    + ' Off</div>'
                    + '</td></tr>');

                // New discount information; save and update totals
                $('#discount-storage').text(data);
                showPrice();
            }
        }
    });
}
2
  • 1
    Post the applyCoupon function - it's probably an async call... Commented Oct 20, 2014 at 19:01
  • can you show the applyCoupon code? Commented Oct 20, 2014 at 19:02

1 Answer 1

1

Since applyCoupon is an async function the rest of your code will keep running while that requests processes. Use a callback to run code after the call has completed:

applyCoupon($('#coupon'), $(this), function() {
    var price = $('#price-total').text();
    price = price.substring(5,price.length);

    var theText = $('#fulldiscounttext').text();  // Sometimes this has a value while debugging, sometimes not
    var discount = theText.substring(1, theText.length-4); // Takes unwanted characters out of string

    var total = price - discount;

    $('#price-total').text('US $ ' + total.toFixed(2));
    var thetotal = $('#price-total').text();
    $('#grandtotal').val(thetotal);
});

And the applyCoupon function:

function applyCoupon(couponInput, couponButton, callback) {
    //AJAXy stuff
    success: function(data, textStatus)
    {
        couponButton.removeAttr('disabled');

        if(data == "bad error code")
            couponOK = 0;
        else
            couponOK = 1;

        if (!couponOK)
        {
            var badcode = couponInput.val().toString();
            if (badcode.length > 0)
            {
                var fmt = 'You\'ve entered an invalid code.';
                $('#coupon-error').text(fmt);
            }
        }
        else    // Coupon recognized!
        {
            $('#total-row').before('<tr><td colspan="4">'
                + '<div id="fulldiscounttext">' + data
                + ' Off</div>'
                + '</td></tr>');

            // New discount information; save and update totals
            $('#discount-storage').text(data);
            showPrice();
        }
        callback(); //ADDED CALLBACK TO RUN ONCE THE AJAX REQUEST IS COMPLETE
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! Been fooling with this for days. Not sure why I didn't come here sooner. I know just enough jQuery to do a few things, but when it gets a bit more complicated, I'm lost. The async call explanation makes sense. Now I need to go learn more about callbacks. I appreciate your time. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.