我有一个表单提交页面,当提交表单时我需要调用Ajax。但是现在Ajax没有用。我的主页是add_sale.php,Ajax页面是ajx_check_sale.php
我的代码:
add_sale.php
function validate_form()
{
var cust_name= $('#cust_name').val();
var total= $('#total').val();
var sale_type= $('#sale_type').val();
if(sale_type=='return')
{
$.ajax({
type: "POST",
url: 'ajx_check_sale.php',
data:'cust_name='+cust_name + '&total=' + total,
success: function(msg)
{
alert(msg);
/*if(msg==0)
{
alert("Return is greater then sale");
return false;
} */
}
});
}
}
<form action="" method="post" name="adFrm" onSubmit="return validate_form()">
</form>
ajx_check_sale.php
require_once("codelibrary/inc/variables.php");
require_once("codelibrary/inc/functions.php");
echo $cust_name=$_POST['cust_name'];
echo $return=$_POST['total'];
$cus="select sum(total) as total_sum from customer where id='$cust_id'";
$cus2=mysql_query($cus);
$fet=mysql_fetch_array($cus2);
$total=$fet['total_sum'];
if($return>$total)
{
$status=0;
echo $status;
}
else
{
$status=1;
echo $status;
}
答案 0 :(得分:1)
您可以更好地使用JQuery侦听器并阻止默认操作,而不是使用onSubmit
调用它:
<script>
$(document).on("submit", 'form[name="adFrm"]',function(e){
e.preventDefault();
var cust_name= $('#cust_name').val();
var total= $('#total').val();
var sale_type= $('#sale_type').val();
if(sale_type=='return')
{
$.ajax({
type: "POST",
url: 'ajx_check_sale.php',
data:'cust_name='+cust_name + '&total=' + total,
success: function(msg)
{
alert(msg);
/*if(msg==0)
{
alert("Return is greater then sale");
return false;
} */
}
});
}
});
</script>
<form action="" method="post" name="adFrm">
<input type="submit" name="yourSubmitButton" />
</form>