Amit Singh
Amit Singh

Reputation: 2275

I have encountered a really weird thing while Inserting jquery var in mysql table from a php page

I have a php page where i have used a jquery function to get the dynamic value according to the values of checkboxes and radio buttons and text boxes. Whats' happening is i have used two alerts

1.) alert(data);

2.)alert(grand_total);

in the ajax part of my Jquery function just to ensure what value i'm getting in "grand_total". And everything worked fine, alerts were good and data was being inserted in the table properly.

Then i removed the alerts from the function, and after sometime i started testing the whole site again and i found value of grand_total in not being inserted in mysql table.

I again put those alerts to check what went wrong, again everything started working fine. Removed again and problem started again. Any idea folks what went wrong?

here is the code snippet of JQUERY func from "xyz.php":

<script type="text/javascript">
$(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: 'xyz_sql.php',
            type: 'POST',
            data: {
                grand_total: grand_total
            },
            success: function(data) {
                // do something;

            }
        });

    });

   });

Corresponding HTML code:

       <form method="post" id="logoform3" action="xyz_sql.php">
       <input type="text" name="Totalcost" id="Totalcost"  disabled/>       
       <input type="submit" id="Next" name="next"/>

This the code from *"xyz_sql.php"*:

<?php
session_start();

include ("config.php");

$uid = $_SESSION['uid'];

$total= mysql_real_escape_string($_POST['grand_total']);

$sql="INSERT INTO form2 (total,uid)VALUES('$total','$uid');";

if($total > 0){
$res = mysql_query($sql);
}

if($res)
{
echo "<script> window.location.replace('abc.php') </script>";
}
else {
echo "<script> window.location.replace('xyz.php') </script>";
}
?>

And last but not the least: echo " window.location.replace('abc.php') "; never gets executed no matter data gets inserted in table or not.

Upvotes: 1

Views: 107

Answers (2)

01e
01e

Reputation: 701

Solutions of Роман Савуляк is good but weren't enough.
You should casting your $total variable to integer in php file and also use if and isset() to power your code, so I'll rewrite your php code:

<?php
session_start();
include ("config.php");
if(isset($_SESSION['uid']))
{
    $uid = $_SESSION['uid'];
    if(isset($_POST['grand_total']))
    {
        $total= mysql_real_escape_string($_POST['grand_total']);
        $sql="INSERT INTO form2(total,uid) VALUES('".$total."','".$uid."')";
        if((int)$total > 0)
        {
            if(mysql_query($sql))
            {
              echo "your output that will pass to ajax done() function as data";
            }
            else 
            {
                echo "your output that will pass to ajax done() function as data";
            }
        }
    }
}

and also you can pass outputs after every if statement, and complete js ajax function like:

$.ajax({
        url: 'xyz_sql.php',
        type: 'POST',
        data: {
            grand_total: grand_total
        }
}).done(function(data) {
            console.log(data);   //or everything
});

Upvotes: 0

Silwerclaw
Silwerclaw

Reputation: 695

First you submit form like form, not like ajax - cause there is no preventDefault action on clicking submit button. That's why it looks like it goes right. But in that form there is no input named "grand_total". So your php script fails.

Second - you bind ajax to element with id "next" - but there is no such element with that id in your html that's why ajax is never called.

Upvotes: 3

Related Questions