停止来自的多个HTML提交 [英] Stop multiple submits for a html from

查看:41
本文介绍了停止来自的多个HTML提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经用谷歌搜索了这个问题,他们都给出了相同的代码,但是它永远无法正常工作.我希望只能单击一次按钮,这样您就不会发送垃圾邮件,也不要单击多次发送该按钮的垃圾邮件.

So I've google this problem, they all give the same code but it never works. I want to be able to only click on the button once so you can't spam click the button sending the from more than once.

这是我的HTML.

<form id="sign" name="sign" method="post">
    <input type="text" name="username" placeholder="Username"><br>
    <input type="text" name="email" placeholder="Email"><br>
    <input type="password" name="password" placeholder="Password"><br>
    <input type="submit" name="submit" value="Signup">

</form>

现在人们在Google上所说的解决方法是将此代码添加到提交输入类型中.

Now the fix people are saying on Google is to add this code to the submit input type.

onclick="this.value='Submitting ..'; this.disabled=true; this.form.submit();"

onclick="this.value='Submitting ..'; this.disabled=true;"

现在,它像我想要的那样在单击了提交按钮之后将其禁用,但是它停止了提交表单,这首先使进行操作的点变得不正确.

Now this disables the submit button after it's been clicked like I want but it stops the form being submitted which defects the point in doing it in the first place.

最后的代码不起作用

<form id="sign" name="sign" method="post">
    <input type="text" name="username" placeholder="Username"><br>
    <input type="text" name="email" placeholder="Email"><br>
    <input type="password" name="password" placeholder="Password"><br>
    <input type="submit" name="submit" value="Signup" onclick="this.value='Submitting ..'; this.disabled=true;">

</form>

推荐答案

您需要使所需的输入之一启用提交"按钮-它变为禁用状态-由init()函数设置.我已经在此处的文本框中进行了说明.

You would need to make one of your required inputs enable the submit button - it arrives disabled - set by the init() function. I have done it for a textbox here to illustrate.

在键入文本框时,您需要在JavaScript中为$hidden_input设置一个值-页面加载时,该值应为0,而准备提交时则应设置为1.您可以用不同的方式进行设置,同时在PHP中也可以设置该值.这完全取决于您希望它如何执行.

You will need to set a value in your JavaScript for $hidden_input when the textbox is typed into - it should be 0 when the page loads but be set to 1 when it is ready to submit. You could set it up in different ways with the value being set in PHP as well. That depends exactly how you want it to perform.

您还应该将表单操作设置为<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>.

You should also set your form action to <?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>.

<?php   
if (isset($_POST['hidden_input'])) $hidden_input = intval($_POST['hidden_input']);

$submit_count_input = intval($_POST['submit_count_input']);

if ($hidden_input == 1){
// do your processing here
$hidden_input = 2;
echo "Thank you. Form processed";
}else{
$hidden_input = 1;
echo "Form not processed.";
}    
// remove this section - just for testing
echo " Control Token: " . $hidden_input . " ";
echo " Submit Count: " . $submit_count_input; // to show it submits but does not process form
$submit_count_input++; 
?>
<!DOCTYPE html>
  <html>
    <head>
    <script language="javascript">
    function init(){
    if (document.getElementById('hidden_input').value == 2){
    document.getElementById('sbutton').value = "   Submitted   ";
    }else{
    document.getElementById('sbutton').value = "   Submit  ";
    }
    document.getElementById('sbutton').disabled = true;
    }
    function enable_it(){
    // could reset it here but you will be able to submit again
    //document.getElementById('hidden_input').value = 1;
    document.getElementById('sbutton').disabled = false;
    document.getElementById('sbutton').value = "   Submit  ";
    }
    function submit_this(){
    document.form.submit();
    }
    </script>
    </head>
    <body onload="init()">

      <form name="form" id="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post">
      <input type="text" name="text_box" onKeyPress="enable_it();" required />

      <input type="hidden" name="hidden_input" id="hidden_input" value="<?php echo $hidden_input ?>" />
      <input type="hidden" name="submit_count_input" id="submit_count_input" value="<?php echo $submit_count_input ?>" />

      <input type="button" name="sbutton" id="sbutton" value="" onclick="submit_this();" />
      </form>
      <a href="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">Click to start a new submission</a>

    </body>
   </html>

您可以使用隐藏输入中的值来阻止将来提交-

You can use the value from the hidden input to block future submission -

 if (intval($_POST['hidden_input']) == 1){
  // skip your signup code
 }

请记住,此页面主要依赖于JavaScript,以防万一对于那些不喜欢JavaScript依赖的人来说可能是个问题.

Bear in mind that this page is mostly dependent on JavaScript - just in case that may be an issue for those who don't like JavaScript dependency.

这篇关于停止来自的多个HTML提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆