0

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);
}
10
  • 12
    Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Sep 3, 2013 at 8:59
  • 1
    Also, no such thing as a jQuery variable. jQuery is JavaScript. Re: sometimes not getting the value, consult your network log. Does it get sent with the request? Commented Sep 3, 2013 at 9:11
  • try this top replace var total = = 0 to this var total = $(this).val(); Commented Sep 3, 2013 at 9:36
  • @Quentin: Thanks i had no idea about the stuff you mentioned, from now on i will definitely keep these in mind. Commented Sep 3, 2013 at 9:45
  • 1
    @sanki take a look for php function header() where you can specify new location: header('Location: http://www.example.com/abc.php'); Commented Sep 27, 2013 at 9:16

1 Answer 1

2

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'));
}
Sign up to request clarification or add additional context in comments.

Comments

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.