guri
guri

Reputation: 684

Before Form submission perform some sql query to insert values into the database

m having a problem while submitting the form using jquery & Ajax. my prob is that i want to insert some values into the database before submitting the form. code that i am using is as below for form submission :

$('.buttons').click(function(){

var account_num = $("#accountNum").val();
var ref_num = $("#refNum").val();

var dataString = "account_num="+ escape(account_num) + "&reference_no=" + escape(ref_num);

$("#frmTransaction").hide();
$("#loader_div").show();

$.ajax({
   type : "Post",
   url  : "validate-data.php",
   data : dataString,
   success: function(html) {
      if(html) {
    alert(html);
    $("#frmTransaction").submit();

      } else {
          alert('Unknown Error Please...Retry');    
       return false;
       }
    }, error :function (){
        return false    
    }, 
       complete: function () {  }

});
 return false;

});

html form is as :

<form  method="post" action="https://sample.com/pay" name="frmTransaction" id="frmTransaction">
<input name="accountNum" id="accountNum" type="hidden" size="60" value="<? echo $_POST['account_id'] ?>">
<input name="refNum" id="refNum" type="hidden" size="60" value="<? echo $reference_no;?>" />
<input type="text" class="field" name="name" id="name" />
<input type="text" class="field" name="city" id="city" />
<input type="text" class="field" name="state" id="state" />
<input type="text" class="field" name="postal_code" id="postal_code" />
<input type="submit" class="buttons" name="submitted" value="Proceed" />
</form>

content of validate-data.php is as :

<?PHP
        $account_num = $_REQUEST['account_num'];
    $reference_no = $_REQUEST['reference_no'];

    $countF = mysql_num_rows(mysql_query("SELECT * FROM orders where order_id='".$reference_no."'"));

    if($countF == 0 ) {
        $res = mysql_query("insert into orders(order_id, user_account_num)values('".$reference_no."', '".$account_num."')");
    } 

if($res) 
    echo "$res ";
else 
    echo "";    
?>

Upvotes: 0

Views: 1778

Answers (2)

Michael Antonius
Michael Antonius

Reputation: 972

only two problem : One, see your callback success:

success: function(html) {
  if(html) {
  alert(html);
  $("#frmTransaction").submit();

  } else {
      alert('Unknown Error Please...Retry');    
   return false;
   }
}

your if statement will ask to computer, if isset variable html and two, you print the mysql query on your php code ...

if($res) 
   echo "$res ";
else 
   echo "";

and the workaround: change your jquery code with this one:

$.ajax({
 type : "Post",
 url  : "validate-data.php",
 data : dataString,
 success: function(html) {
 var callback = $.parseJSON(html); // Parse your callback
   if(callback.process == 'success'){
      $("#frmTransaction").submit();
   } else {
      alert('Unknown Error Please...Retry');    
   }
 }, error:function(){
    alert('Unknown Error Please...Retry');    
 }, 
});

and the php code:

<?PHP
 $account_num = mysql_real_escape_string($_POST['account_num']); // make sure `$_POST` and `mysql_real_escape_string` used to prevent sql injection
 $reference_no = mysql_real_escape_string($_POST['reference_no']); // And this line too

 $countF = mysql_num_rows(mysql_query("SELECT * FROM orders where   order_id='".$reference_no."'"));

if($countF == 0 ) {
    $res = mysql_query("insert into orders(order_id,   user_account_num)values('".$reference_no."', '".$account_num."')");
} 

if($res){
  echo json_encode(Array("process" => "success"));
}
else {
  echo json_encode(Array("process" => "fail")); 
}
?>

Good luck, Friend

Upvotes: 1

MrCode
MrCode

Reputation: 64526

Your form doesn't submit because in the Javascript for the submit button click, you return false at the end. That means the form will be prevented from submitting, so remove that line completely.

I also noticed you're building the raw post data as a string, you can do that but it's easier to let jQuery take care of it, you can pass an object literal instead and forget about the encoding:

$.ajax({
    .....
    data : { 'account_num' : account_num, 'reference_no' : ref_num },
    .....

In the PHP you are echoing a MySQL result resource. Instead you should echo something like a boolean 1 or 0, or JSON encode a response object.

echo $res ? '1' : '0';

The above is a ternary operator which is saying, if the query was successful then echo 1, if not then echo 0.

Upvotes: 1

Related Questions