Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQueryPost使用$.ajax,如何避免多次发布?_Php_Jquery_Http Post - Fatal编程技术网

Php jQueryPost使用$.ajax,如何避免多次发布?

Php jQueryPost使用$.ajax,如何避免多次发布?,php,jquery,http-post,Php,Jquery,Http Post,仍在寻找解决方案,但尚未找到,我有一个功能可以管理相同/不同页面上的不同表单 function formStantardAction(correctAnswer,addCustomData){ addCustomData = (typeof addCustomData == "undefined")?'':addCustomData; correctAnswer = (typeof correctAnswer == "undefined")?'Saved.':correc

仍在寻找解决方案,但尚未找到,我有一个功能可以管理相同/不同页面上的不同表单

function formStantardAction(correctAnswer,addCustomData){
        addCustomData = (typeof addCustomData == "undefined")?'':addCustomData;
    correctAnswer = (typeof correctAnswer == "undefined")?'Saved.':correctAnswer;
    $('form.standard').submit(function(event){
        event.preventDefault();
        var modalWin = $(this).parent();
        var values = $('form.standard').serialize() + addCustomData;
        $.ajax({
        url: "inc/gateway.php",
        data: values,
        type: "POST",
        success: function(data){
            if (data == "OK"){
                $(modalWin).html(correctAnswer).delay(500).fadeOut(500);
                setTimeout(function() {
                    mw_close();
                }, 1000);
            }else{
                alert(data);
            }
        }
        });

    });
    }
使用名为SEND的输入type=“button”加载页面和表单后

$('form.standard [name="SEND"]').click(function(){
            var str = $('#sortableTo').serializelist();
        formStantardAction('New train inserted.',str);
            $('form.standard').submit();
        });
所有值都通过POST到达php页面,该页面生成所有内容(验证、在db中插入、更新日志…),如果所有内容都正常,则回答“OK”(因此模式窗口中的表单被自定义消息和淡出替换)或。。。如果有错误,php会用js弹出的一些文本来回答,并发出警告,使模式窗口与表单一起打开

这一切都没问题,但是,如果php回答有错误,第二次点击发送按钮,帖子会被发送2次。 如果我在第二次发送时再次出错,并再次单击发送按钮,则post值将发送三次。。。等等

我能做什么?我的错误在哪里


谢谢。

每次从ajax请求中获取数据时,是否有可能在代码中的某个地方将提交事件绑定到表单


我无法在您提交的代码中对此进行检查。

创建一个全局变量作为标志

var flag = 0;
并在发布时检查此标志,完成后将其重置

function formStantardAction(correctAnswer,addCustomData){
       **if(flag == 1){
           return false;
       }
       flag = 1;**
        addCustomData = (typeof addCustomData == "undefined")?'':addCustomData;
    correctAnswer = (typeof correctAnswer == "undefined")?'Saved.':correctAnswer;
    $('form.standard').submit(function(event){
        event.preventDefault();
        var modalWin = $(this).parent();
        var values = $('form.standard').serialize() + addCustomData;
        $.ajax({
        url: "inc/gateway.php",
        data: values,
        type: "POST",
        success: function(data){
           **flag = 0;**
            if (data == "OK"){
                $(modalWin).html(correctAnswer).delay(500).fadeOut(500);
                setTimeout(function() {
                    mw_close();
                }, 1000);
            }else{
                alert(data);
            }
        }
        });

    });
    }

我在submit函数的开头添加了一些自定义代码——基本上,如果正在进行提交,则不应执行任何操作,否则会像往常一样返回错误消息

var submitting = false; //initialise the variable, this needs to be out of the function!
function formStantardAction(correctAnswer,addCustomData){
    addCustomData = (typeof addCustomData == "undefined")?'':addCustomData;
correctAnswer = (typeof correctAnswer == "undefined")?'Saved.':correctAnswer;

$('form.standard').submit(function(event){
    if (submitting) {
        return false; //if a submit is in progress, prevent further clicks from doing anything
    } else {
        submitting = true; //no submit in progress, but let's make one now
    }
    event.preventDefault();
    var modalWin = $(this).parent();
    var values = $('form.standard').serialize() + addCustomData;
    $.ajax({
    url: "inc/gateway.php",
    data: values,
    type: "POST",
    success: function(data){
        if (data == "OK"){
            $(modalWin).html(correctAnswer).delay(500).fadeOut(500);
            setTimeout(function() {
                mw_close();
            }, 1000);
        }else{
            alert(data);
        }
    }
    });

});
}

尝试排除提交块:

 function formStantardAction(correctAnswer,addCustomData){
        addCustomData = (typeof addCustomData == "undefined")?'':addCustomData;
    correctAnswer = (typeof correctAnswer == "undefined")?'Saved.':correctAnswer;
    //$('form.standard').submit(function(event){
     //   event.preventDefault();
//change 'this' to form.standard
        var modalWin = $('form.standard').parent();
        var values = $('form.standard').serialize() + addCustomData;
        $.ajax({
        url: "inc/gateway.php",
        data: values,
        type: "POST",
        success: function(data){
            if (data == "OK"){
                $(modalWin).html(correctAnswer).delay(500).fadeOut(500);
                setTimeout(function() {
                    mw_close();
                }, 1000);
            }else{
                alert(data);
            }
        }
        });

    });
   // }
加载页面后:

   $('form.standard [name="SEND"]').click(function(){
                var str = $('#sortableTo').serializelist();
            formStantardAction('New train inserted.',str);
//excluding submit event
               // $('form.standard').submit();
            });
因为类型为“Post”的$.ajax{}已经是一个提交过程,当脚本调用submit时,它将重新提交


希望这是正确的,并提供帮助

不确定这是否会产生影响,但如果运行异步后处理导致了问题,请尝试在ajax设置上设置
async:false
,看看这是否会产生影响。只需在setTimeout函数后添加return false,看看是否停止它。@FrançoisWahl Hi,谢谢你的建议,但只是尝试一下,什么都不会改变。@RavinderSingh谢谢你的建议,但尝试在function的末尾(在$.ajax之后)添加return false in SUCCESS,in error answer,但做同样的事情error@keebOo:抱歉,它不起作用,但这就是为什么我只是添加它作为评论,因为我不确定。我很高兴你最终解决了:)对不起。。。我不明白,我是说。。。1) 单击发送按钮2)获取当前表单值+用户设置的一些自定义值3)调用主函数更改数据并提交表单操作,打开ajax连接并wai应答4)将数据放在DB上并应答OK或应答有误,然后等待新的发送。。。。。。。我把它放在哪里?和以前一样。问题是,始终是成功的,有“确定”答案和“错误”答案,但如果出现错误,表单将保持打开状态,当我单击“发送”按钮发送两次时。。。(从php返回两次错误)您可以在错误上设置flag=0,也可以像success jquery ajax have errormmmmm。。。我检查一下,这可能更接近答案。查看主函数后的最后几行代码:每次单击按钮1)我都会得到表单的值2)再次编写提交函数3)提交表单…这可能是错误吗?是的!是的(提交功能用于管理表单,没有自定义按钮,只有提交按钮……但我会添加一段新代码)非常感谢。