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);
}
Amit Singh
  • 2,267
  • 4
  • 25
  • 50
  • 12
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 03 '13 at 08: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? – Mitya Sep 03 '13 at 09:11
  • try this top replace var total = = 0 to this var total = $(this).val(); – Neelesh Sep 03 '13 at 09:36
  • @Quentin: Thanks i had no idea about the stuff you mentioned, from now on i will definitely keep these in mind. – Amit Singh Sep 03 '13 at 09:45
  • @Utkanos: Then what shall i call it? – Amit Singh Sep 03 '13 at 09:45
  • @Neelesh: $(this).val(); will only assign current value to var_total, howz that gonna effect the purpose. And i did var_total since initially i want var_total to have 0. – Amit Singh Sep 03 '13 at 09:49
  • I was getting value of $("#Totalcost") instead of 0. I couldn't find any issue in your code, because grand_total initialized by 0 . so no chance if you are not getting value some time. – Neelesh Sep 05 '13 at 10:01
  • @Neelesh: i guess there is some problem regarding mysql_* functions in my abc_sql.php page. BTW thanks for devoting your time :-) – Amit Singh Sep 05 '13 at 10:06
  • 1
    @sanki take a look for php function [header()](http://php.net/manual/en/function.header.php) where you can specify new location: `header('Location: http://www.example.com/abc.php');` – super pantera Sep 27 '13 at 09:16
  • previously i tried that only but no luck..with that too :( – Amit Singh Sep 27 '13 at 10:04

1 Answers1

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'));
}
Leo Nyx
  • 696
  • 3
  • 5