我的ajax调用将一条记录输入到数据库中(这是记录数据的表单的第一部分),因此我需要它从数据库条目中返回id。
问题是,它触发了两次,所以每次生成2个数据库条目。
我试着在我的php代码中使用$count
和while($count>0)
来确保它不会循环,但我不认为是循环,所以问题出在我的jQuery中。
我试着把preventDefault
放在我的提交按钮点击功能上,但也没用。
这是我的代码:
$(document).ready(function(){
$('#wpgstep1').one('click',function(){
// validate form fields are all filled in
var budget=$('#budget').val();
if(budget=='')
{
$('#budgeterror').show();
}
var yellowpages=$('#ads-yellowpages').val();
var flyers=$('#ads-flyers').val();
var brochures=$('#ads-brochures').val();
var radiotv=$('#ads-radiotv').val();
var none=$('#ads-none').val();
var other=$('ads-other').val();
var otherstatement=$('ads-other-statement').val();
var cust_id=$('#cust_id').val();
if(other !='')
{
if(otherstatement==='')
{
$('#adsothererror').show();
}
}
else
{
otherin='0';
}
if(yellowpages==="on")
{
yellowpagesin='1';
}
else{
yellowpagesin='0';
}
if(flyers==="on")
{
flyersin='1';
}
else
{
flyersin='0';
}
if(brochures==="on")
{
brochuresin='1'
}
else
{
brochuresin='0';
}
if(radiotv==="on")
{
radiotvin='1';
}
else
{
radiotvin='0';
}
if(none==="on")
{
nonein='1'
}
else
{
nonein='0';
}
var dataString='cust_id=' + cust_id + '&step=1&budget=' + budget + '&yellowpages='+yellowpagesin + '&flyers=' + flyersin + '&brochures=' + brochuresin + '&radiotv='+ radiotvin + '&none='+ nonein + '&other=' + otherstatement;
$.ajax({
type: "POST",
url: "submitwpg.php",
data: dataString,
dataType:'json',
success: function(data)
{
alert(data);
var i="";
var p=eval (data);
for (i in p)
{
$('#wpgpart2').append('<input type=hidden name=wpgid value=' + p[i] + '>');
}
$('#wpgform1').hide();
$('#wpgform2').show();
}
});
return false;
});
});
生成全局var
var form_submitting = false;
在您的ajax调用之上
if(form_submitting == false){
form_submitting = true;
//your ajax call
}
在您的ajax成功函数中调用
form_submitting = false;
如果您的提交按钮在表单中,则可能是您的ajax函数正在执行,然后表单定期发布。你可以试着转动
<input type='submit' />
进入
<button type='button' onclick='validateAndAjaxFunction(); return false;'></button>