简体   繁体   English

禁用提交按钮时,PHP 重定向不起作用

[英]PHP redirect doesn't work when submit button is disabled

In my index.php page I have a submit button which takes form data and sends it through POST to an update.php page.在我的 index.php 页面中,我有一个提交按钮,它接收表单数据并通过 POST 将其发送到 update.php 页面。 On this new page, the database is manipulated then redirects (with PHP) back to the original page.在这个新页面上,对数据库进行操作,然后(使用 PHP)重定向回原始页面。 This redirection process works fine in Firefox, but does not seem to work in Chrome/IE.此重定向过程在 Firefox 中运行良好,但在 Chrome/IE 中似乎不起作用。

Relevant code in index.php file: index.php文件中的相关代码:

<form name="battle" action="update.php" method="post">
<input type="radio" name="update" value="<?php print($names[0] . "+" . $names[1]); ?>" />
<input type="radio" name="update" value="<?php print($names[1] . "+" . $names[0]); ?>" />
<input type="submit" name="submit" value="Submit" />
</form>

This is the code at the end of my update.php file:这是我的 update.php 文件末尾的代码:

$con->close();
session_write_close();
header("Status: 200");
header( "Location: index.php" );
exit(0);

This code will work in all browsers if the submit button is NOT disabled onClick, but I really need to have this feature.如果提交按钮禁用 onClick,此代码在所有浏览器中工作,但我确实需要此功能。 Does anyone know how to make this redirect work while still having the disable button fetaure?有谁知道如何在仍然具有禁用按钮功能的同时使此重定向工作?

I'm guessing you're disabling the submit button with javascript to prevent multiple submits.我猜您正在使用 javascript 禁用提交按钮以防止多次提交。 If you're doing it by setting the disabled property of the submit button then this isn't the best way of doing it as it can have side-effects.如果您通过设置提交按钮的禁用属性来执行此操作,那么这不是最好的方法,因为它可能会产生副作用。

If you want to prevent multiple submits with javascript, a better way of doing it is to attach an event handler to the form submit event that only returns true the first time you trigger the submit event.如果您想使用 javascript 防止多次提交,更好的方法是将事件处理程序附加到表单提交事件,该事件处理程序仅在您第一次触发提交事件时返回 true。

function submitHandler ()
{
    if (typeof submitHandler.triggered == 'undefined')
    {
        submitHandler.triggered = true;
        return true;
    }
    else
    {
        return false;
    }
}

Attach the above to your form's submit event.将上述内容附加到表单的提交事件中。 This should prevent multiple submits and also avoid the problems that disabling the submit button can cause.这应该可以防止多次提交,也可以避免禁用提交按钮可能导致的问题。

Disabling submit buttons on form submits can be a tricky business.禁用表单提交上的提交按钮可能是一件棘手的事情。 This worked for me:这对我有用:

NB: Requires >= jQuery1.6 to work注意:需要 >= jQuery1.6 才能工作

test.php:测试.php:

<script type="text/javascript">
$(document).ready(function ()
{
    $("form").submit(function ()
    {
        $("input[type='submit']").prop("disabled", true);
    })
})
</script>

<form name="battle" action="update.php" method="post">
    <input type="radio" name="update" value="foo" />
    <input type="radio" name="update" value="bar" />
    <input type="submit" name="submit" value="Submit" />
</form>

update.php:更新.php:

header("Status: 200");
header( "Location: index.php" );
exit(0);

?>

I recently had the same problem and here is what i did to fix, in all browsers我最近遇到了同样的问题,这是我在所有浏览器中所做的修复

$(document).ready(function () {
     $("input[type='submit']").click(function () {
         $(this).attr('disabled','true');
         // validate data
         ...

         // when data is valid send form else enable form
         $('form[name="battle"]').submit();
      })

 });

now your form data will be sent in any browser.现在您的表单数据将在任何浏览器中发送。 try and let me know how it goes:)试着让我知道它是怎么回事:)

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

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