简体   繁体   English

使用按钮防止多次提交

[英]prevent multiple submission with button

I am currently viewing all the possibilities for preventing multiple submission with button tag. 我目前正在查看所有防止使用按钮标签进行多次提交的可能性。 The problem I am facing is that if users click submit button really fast it will enable them to submit multiple posts. 我面临的问题是,如果用户真正快速单击“提交”按钮,它将使他们能够提交多个帖子。 I would like to restrict the submission to just one submission. 我想将提交内容限制为仅提交一个。 I tried to use onclick="this.disabled = true , but it makes the button not working at all. The current button tag looks like this: 我尝试使用onclick="this.disabled = true ,但是它使按钮根本无法工作,当前的按钮标签如下所示:

return "<button class='button btn btn-primary' id='gform_submit_button' onclick='this.disabled = true' type='submit'><span>Submit!/span></button>";

Can anyone guide me as to how to achieve this? 谁能指导我如何实现这一目标?

Ultimately, you cannot prevent multiple submissions on the client-side. 最终,您不能在客户端阻止多个提交。 You would have to implement these security measures on the server-side, in whatever server-side language you are using (eg, PHP). 您将必须以任何使用的服务器端语言(例如PHP)在服务器端实施这些安全措施。

On the client side , you could do something like this 客户端 ,您可以执行以下操作

var canSubmit = true;

$('.button').click(function(){

    if(canSubmit)
    {
        // fire missiles
        canSubmit = false;
    }
    else
    {
        // sorry missiles loading
    }

});

Now since after clicking once canSubmit has been set to false , a second click would not run the code. 现在,由于在单击一次canSubmit其设置为false ,因此第二次单击将不会运行该代码。 After validating or processing your submitted data you can set canSubmit back to true . 验证或处理提交的数据后,可以将canSubmit设置为true

When the button is onClicked call this function: 当按钮为onClicked时,请调用此函数:

function submitFunc(formId){.    document.getElementById(formId).submit();
}

Submitting a page is always going to be tricky. 提交页面总是很棘手。 There are two challenges with submit 提交有两个挑战

  1. As you rightly mentioned that user can submit same page multiple times 正如您正确提到的,用户可以多次提交同一页面
  2. After submitting page if user refresh the page then also page is going to be resubmitted 提交页面后,如果用户刷新页面,则该页面也将重新提交

There is one trick to handle this challenge redirect the page with GET call. 有一个技巧可以解决此挑战,可以使用GET调用重定向页面。 The GET call which you have used to load the data. 您用来加载数据的GET调用。 Read more about it here . 在此处了解更多信息。

So I would recommend to redirect page to GET once form is submitted. 因此,我建议在提交表单后将页面重定向到GET。

In this process the new form will be loaded and if user try to submit the form validations will be fired that will handle 1st challenge. 在此过程中,将加载新表单,如果用户尝试提交表单,则将触发将处理第一个挑战的验证。

And due to redirect as your last call is GET on refresh data will be loaded and there is no harm in it. 并且由于重定向,因为您的最后一次调用是GET,刷新数据将被加载,并且其中没有任何危害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM