我在使用 jquery 和 Ajax 提交表单时遇到问题。我的问题是我想在提交表单之前向数据库中插入一些值。我正在使用的代码提交表单如下:
$('.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 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>
validate-data.php 的内容如下:
<?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 "";
?>