简体   繁体   English

我怎样才能让我的提交按钮点击一次并仍然执行我的 php?

[英]how can i make my submit button clickable once and still execute my php?

I have tried solving this but no matter what i try the button doesn't stay disabled for more than a second and won't execute my PHP script.我试过解决这个问题,但无论我尝试什么,按钮都不会保持禁用状态超过一秒钟,也不会执行我的 PHP 脚本。

I have a restart button on my web page that when clicked sends an email to the admin to let them know a restart has been requested.我的网页上有一个重启按钮,点击后会向管理员发送一封电子邮件,让他们知道已请求重启。 I want this to be able to be clicked once then become greyed out to prevent multiple clicks.我希望它能够被点击一次,然后变灰以防止多次点击。 I want it to become active again after a certain amount of time我希望它在一段时间后再次活跃

How can I get the Restart button to become clickable once (to rule out the possibility for too many script executions) and still execute the PHP script?如何让重新启动按钮变为可点击一次(以排除执行过多脚本的可能性)并仍然执行 PHP 脚本?

This is my code:这是我的代码:

<form action=index.php method= "post">
<input type="submit" value="Request Restart" id="alertTimerButton" name="clickMe" onclick="countClicks();"/>

<?php
if (isset($_POST['clickMe']))
{
    exec("/anaconda3/bin/python /Users/shoot_station_5/Sites/simpleblogs/restartemail.py \"$input_val\""); 
} else{

}

I have tried adding various javascript such as:我尝试添加各种 javascript,例如:

function disable()
  $(document).ready(function(){
      $('.once-only').submit(function(){
      $(this).children('button').prop('disabled', true);
      alert("Thankyou a restart has been requested.");
  });
});

and

<script type="text/javascript">
  var ClickCount = 0;
  function countClicks() {
    var clickLimit = 1; //Max number of clicks
    if(ClickCount>=clickLimit) {
      alert("a restart has already been requested");
      return false;
    }
    else
    {
      ClickCount++;
      alert("Thankyou a restart has been requested.");
      return true;
    }
  }

You have to change你必须改变

onclick="countClicks();"

to

onclick="return countClicks();"

then your 2. solution does work.那么您的 2. 解决方案确实有效。

Your 1. solution has multiple bugs.您的 1. 解决方案有多个错误。 Don't wrap the code in a disable() function.不要将代码包装在 disable() 函数中。 And there is a # missing before 'button'.并且在“按钮”之前缺少一个#。

I would do it like this:我会这样做:

onclick="disabled=true;" 

That's enough.就够了。

Because you commented that you don't get your second solution running, here is the code:因为您评论说您没有运行第二个解决方案,所以这里是代码:

 var ClickCount = 0; function countClicks() { var clickLimit = 1; //Max number of clicks if (ClickCount >= clickLimit) { alert("a restart has already been requested"); return false; } else { ClickCount++; alert("Thank you a restart has been requested."); return true; } }
 <form action="#" method="post"> <input type="button" value="Request Restart" id="alertTimerButton" name="clickMe" onclick="return countClicks();"/> </form>

Well the button is working as intended.那么按钮按预期工作。 Since you don't use ajax/xhr here you reload your page when the form is submitted.由于您在此处不使用 ajax/xhr,因此您会在提交表单时重新加载页面。 That's why the button is resetted.这就是按钮被重置的原因。 If you don't want to use ajax/xhr just save your "status" and maybe the time in a session variable and change the button property according to it.如果您不想使用 ajax/xhr,只需保存您的“状态”和会话变量中的时间,然后根据它更改按钮属性。 Your html/php would look like this你的 html/php 看起来像这样

session_start();
if (isset($_POST['clickMe'])) {
    $_SESSION['restarted'] = time();
}
session_write_close();
$timeout = 300; // 5 min
$timer = time();
$disabled = false;
if (isset($_SESSION['restarted']) && ($timer - $_SESSION['restarted']) < $timeout) {
    $disabled = true;
}

.. (html stuff) ...
<form action=test.php method= "post">
    <input type="hidden" name="timer" value="<?php echo (isset($_SESSION['restarted']) ? $_SESSION['restarted'] : null)?>" />
    <input type="submit" <?php echo ($disabled ? 'disabled="true"' : null)?> value="Request Restart" id="alertTimerButton" name="clickMe"/>
</form>

And your js like this (if you use jquery)而你的 js 是这样的(如果你使用 jquery)

$(document).ready(function(){
    $('form').on('submit', function(e){
        alert("Thankyou a restart has been requested.");
        return true;
    });

    var restart_time = parseInt($('input[name="timer"]').val());
    var cur_time;
    setInterval(function(){ 
        cur_time = Math.floor(Date.now() / 1000);
        if (cur_time - restart_time > 300000) {
            $('#alertTimerButton').prop('disabled', false);
        }
    }, 5000);
});

Edit: I fixed the code a bit and added a time check in javascript.编辑:我稍微修复了代码并在 javascript 中添加了时间检查。

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

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