$('.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 "";
?>
答案 0 :(得分:1)
您的表单未提交,因为在提交按钮click
的Javascript中,您最后是return false
。这意味着将阻止表单提交,因此请完全删除该行。
我还注意到你正在将原始帖子数据构建为一个字符串,你可以这样做但是让jQuery更容易处理它,你可以传递一个对象字面而忘记编码:
$.ajax({
.....
data : { 'account_num' : account_num, 'reference_no' : ref_num },
.....
在PHP中,您正在回显MySQL结果资源。相反,你应该回应像布尔1
或0
,或JSON编码响应对象。
echo $res ? '1' : '0';
上面是一个三元运算符,如果查询成功,那么回显1
,如果没有则回显0
。
答案 1 :(得分:1)
只有两个问题:
一,看你的回调success:
success: function(html) {
if(html) {
alert(html);
$("#frmTransaction").submit();
} else {
alert('Unknown Error Please...Retry');
return false;
}
}
你的if语句会询问计算机,如果是isset变量html
二,你在php代码上打印mysql查询......
if($res)
echo "$res ";
else
echo "";
和解决方法: 用这个改变你的jquery代码:
$.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');
},
});
和php代码:
<?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"));
}
?>
祝你好运,朋友