Php Ajax在使用jquery提交表单时不起作用
我有一个表单提交页面,当表单提交时,我需要调用Ajax。但是现在Ajax不起作用了。我的主页是add_sale.php,Ajax页面是ajx_check_sale.phpPhp Ajax在使用jquery提交表单时不起作用,php,jquery,ajax,Php,Jquery,Ajax,我有一个表单提交页面,当表单提交时,我需要调用Ajax。但是现在Ajax不起作用了。我的主页是add_sale.php,Ajax页面是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'];
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;
}
我的代码:
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>
您可以更好地使用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>
$(文档)。在“提交”上,在“表格[name=“adFrm”]”上,在函数(e)上{
e、 预防默认值();
var cust_name=$('#cust_name').val();
var-total=$('#total').val();
var sale_type=$('#sale_type').val();
如果(销售类型=='return')
{
$.ajax({
类型:“POST”,
url:'ajx_check_sale.php',
数据:'cust_name='+cust_name+'&total='+total,
成功:功能(msg)
{
警报(msg);
/*如果(msg==0)
{
警惕(“回报大于销售”);
返回false;
} */
}
});
}
});
错误是什么?也许它不会成功。尝试添加错误回调。因此,我们可以区分问题所在。按F12检查网络选项卡,然后单击该选项卡中的ajax函数并将其粘贴到此处。angular.min.js.map ibnjmihbbanannlbobkbmnmckjnmdnom/lib GET(failed)net::ERR_FILE_NOT_找到其他0 B 0 B 2 ms-jquery.min.map ibnjmihbbanannlbobkbmnmckjnmdnom/lib GET(失败)net::ERR_FILE_NOT_找到其他0 B 0 B 1 ms-angular-route.min.js.map ibnjmihbbanannlbobkbmnmckjnmdnom/lib GET(失败)net::ERR_FILE_NOT_找到其他0 B 0 B待决+1。我不知道这是否是代码中唯一的问题。实际上,操作不是catch(preventDefault()
,return;
)。此外,onSubmit()
中缺少分号。仅供参考:F12是您的朋友:)它不是执行AJAX调用还是您得到了错误的响应?请用add_sale.php
friend,add_sale.php中的全部代码更新您的问题,如果有更多代码,超出堆栈溢出中的代码post限制Uncaught TypeError:undefined不是函数add_sale.php:202 Uncaught TypeError:无法读取未定义add_sale.php:384的属性“old_password”,因此您尝试读取未定义变量的属性。未正确分配具有旧密码的属性。