1

我的 ajax 调用将一条记录输入数据库(它是记录数据的表单的第一部分),所以我需要它从数据库条目中返回 id。

问题是,它触发了两次,所以每次都会创建 2 个数据库条目。

我尝试在我的 php 代码中使用$countandwhile($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;
    });

});
4

2 回答 2

1

制作一个全局变量

var form_submitting = false;

在你的 ajax 调用之上

if(form_submitting == false){
    form_submitting = true;

    //your ajax call

}

在您的 ajax 调用的成功函数中

form_submitting = false;
于 2013-07-28T22:21:25.380 回答
0

如果您的提交按钮位于表单内,则您的 ajax 函数可能正在执行,然后您的表单正在定期发布。你可以试着转动你的

<input type='submit' />

进入

<button type='button' onclick='validateAndAjaxFunction(); return false;'></button>
于 2013-07-28T22:28:14.130 回答