Sickaaron
Sickaaron

Reputation: 448

Send data to another function JQuery

I am trying to set up a coupon/promo/discount codes in my order form, however i cannot get the coupon amount to go to the updatePrices function.

function updateCoupon() {
var coupon = $('#coupon').val();
if (coupon.length) {
    $('#couponContainer .checking').show();         
    $.ajax({
        url: "/includes/pscript/coupon.php",
        data: {coupon: coupon},
        dataType: 'json',
        type: 'POST',
        success: function(data) {
            if(typeof data.available != 'undefined' && data.available == true) {
                    coupon_discount = new Number(data.price.discount);
                    $('#couponStatus').html('<p>'+data.status+'</p > ');
                    $('#couponContainer .checking').hide();
                    $('#couponStatus').show();
                    updatePrices(coupon_discount);
            }  else {
                    coupon_discount = 0;
                    $('#coupon ').val('');
                    $('#couponContainer .checking').hide();
                    $('#couponStatus ').hide();
                    $('#couponStatus ').html('');
                    alert('Sorry this discount code is no long valid!');
            }
        },
        error: function() {
                coupon_discount = 0;
                $('#coupon ').val('');
                $('#couponContainer .checking').hide();
                $('#couponStatus ').hide();
                $('#couponStatus ').html('');
                alert('Sorry this discount code is no long valid!');
        }
    });
}
else
{
    coupon_discount = 0;
    $('#coupon').val('');
    $('#couponContainer.checking ').hide();
    $('#couponStatus ').hide();
    $('#couponStatus ').html('');
    alert('Sorry this discount code is no long valid!');
}

}

The above does work, and it receives the amount. Once it has the amount i would like it to send to updatePrices();

function updatePrices(a, coupon_discount) {

var item_price = 0;
var delivery_price = 0;
var discounts = 0;
if(coupon_discount != 0){
    var coupon_discount = coupon_discount;
    alert('Coupon '+coupon_discount);
}
var product = $("[name='Product']").val();

...

if(coupon_discount != 0)
    {
        discounts += coupon_discount;
    }

    var total = (item_price + delivery_price - discounts);

}

This is only part of the code. All i would like it to do is, updateCoupon gets the price sends it to updatePrices - if say a product was changed the coupon is removed and a message will appear to reapply.

Problem currently: I am getting undefined in updatePrices()

Upvotes: 0

Views: 225

Answers (1)

optimisticupdate
optimisticupdate

Reputation: 1689

You are calling updatePrices(coupon_discount); and your function is defined as updatePrices(a, coupon_discount). Currently your value should be at the a var. Whats the a for?

Upvotes: 1

Related Questions