tags:

views:

54

answers:

3

Could anybody help with this?

I am trying to integrate a coupon function into the order process on my website using jQuery to post the coupon code used and check the coupon code used exists in my database. However jQuery post will not work correctly. Here is my code.

jQuery(function () {
    jQuery("#discount-code").click(function () {
        var coupon = jQuery("#CouponCode").val();
        if (coupon) {
            alert("Coupon Used");
            jQuery.ajax({
                success: function (data) {
                    if (data) {
                        alert("DATA RECEIVED");
                    }
                },
                data: 'coupon=' + coupon,
                type: 'POST',
                dataType: 'json',
                url: 'http://example.com/process-coupon.php'
            });
        }
        return false;
    });
});

I am getting the coupon used alert but after that nothing. I am not even seeing the post info in firebug.

Can anybody see the problem here?

+2  A: 

The problem you describe usually (not even a POST visible in Firebug) comes down to one simple problem named Same origin policy. But this is nothing but a guess (though a very good one) until you post your HTML.

Henrik P. Hessel
probably right, in which case the solution is to create a proxy script on your server that just fetches the URL you pass to it, plus all POST'd variables of course.
burkestar
Yeah, like skymook mentions JSONP is very good choice.
Henrik P. Hessel
Thanks for this answer it helped me fix the problem. My website uses wordpress however my order form is in a folder on the main domain which is not part of the wordpress files. i was trying to post the data to a file contained inside a wordpress plugin. Once I moved the file to the external folder which contains the order form it fixed the problem.
Paul Atkins
A: 

Have you tried setting the datatype to 'jsonp'? It may be that the code is not happy that your script is on the same server. Worth trying as a test.

skymook
A: 

There's also likely a problem with your data.

data : 'coupon='+coupon

Doesn't produce json. Check out http://www.json.org/js.html and use something like:

data : JSON.stringify({'coupon':coupon})

To generate your data object.

ablerman