AKor
AKor

Reputation: 8882

jQuery Ajax POST doesn't go through

I'm trying to do a fairly simple $.post() with jQuery. I have a typical form with inputs, and a button at the end with id #subBuy.

Here's my jQuery:

    $('#subBuy').live('click',function() {
        $.post('php/buyCoup.php', $('#buyCoup').serialize(), function(){
            $('#buyCoup').hide();
            $('#success').show();
        });
    });

When I click the button, it should post the form with Ajax and show/hide certain elements upon success.

I know the HTML is fine because the form submitted without jQuery before.

Here is buyCoup.php:

<?php
    session_start();
        $con = mysql_connect("localhost","foo","bar");
        mysql_select_db("coupons", $con);

    $retailerName = $_REQUEST["retailerName"];
    $intDeal = $_REQUEST["intDeal"];
    $coupTerms = $_REQUEST["coupTerms"];
    $datePicker = $_REQUEST["datepicker"];

    mysql_query("correctly functioning query to insert the data");
?>

Any help?

Upvotes: 0

Views: 153

Answers (1)

Tom Gruner
Tom Gruner

Reputation: 9635

Inside buyCoup.php, you would also want to return something maybe json like {"success" : 1} and send a json header too from php.

If you don't return something to the jquery ajax call, it will probably not call your on complete function.

At the end of buyCoup.php, try adding this this:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

echo '{"success": 1}';

Upvotes: 2

Related Questions