Amit Singh
Amit Singh

Reputation: 2275

Pass JQuery variable to another PHP page

I have a PHP page, Let's say abc.php. I have used jQuery/ajax to post the variable "var_post" to another page named abc_sql.php. But unfortunately sometimes I'm getting that variable on the next page and sometimes not. Any idea what's wrong here?

abc.php Code Snippet:

$(document).ready(function () {

    var grand_total = 0;

    $("input").live("change keyup", function () {

        $("#Totalcost").val(function () {

            var total = 0;
            $("input:checked").each(function () {

                total += parseInt($(this).val(), 10);
            });
            var textVal = parseInt($("#min").val(), 10) || 0;

            grand_total = total + textVal;

            return grand_total;
        });

    });
    $("#next").live('click', function () {

        $.ajax({
            url: 'abc_sql.php',
            type: 'POST',
            data: {
                var_post: grand_total
            },
            success: function (data) {
             }
        });
    });
});

abc_sql.php:

$total = $_POST['var_post'];
$sql = "INSERT INTO total_tab (total)VALUES('$total')";
if ($total > 0) {
    $res = mysql_query($sql);
}

Upvotes: 0

Views: 2412

Answers (1)

Leo Nyx
Leo Nyx

Reputation: 696

You have an empty success callback in your ajax call. Whatever you receive from server, you just discard it. <script> printed in PHP is never executed by browser, because you never add it to the DOM.

Try this as your success callback:

success: function (data) {
    $('<div></div>').html(data).appendTo($('body'));
}

Upvotes: 2

Related Questions