简体   繁体   English

PHP不处理表格

[英]PHP doesn't process the form

I'm having a little problem here. 我在这里有一个小问题。
I have a form with a jQuery that disables the submit input when the form is submited. 我有一个带有jQuery的表单,该表单在提交表单时会禁用提交输入。
The problem is: I use PHP to process the data when the input submit is clicked. 问题是:单击输入提交时,我使用PHP处理数据。 But apparently the button is being disabled first, then PHP can not perform the processing. 但是显然该按钮首先被禁用,然后PHP无法执行该处理。 I need to find a way to prevent this, take a look in the code snippets: 我需要找到一种方法来防止这种情况,请看一下代码片段:

<form class='send' action='{$_SERVER['PHP_SELF']}' method='post'>
    <!--Form data-->
    <input type='submit' name='finish' value='Send'/>
</form>

jQuery function to disable: jQuery功能禁用:

$('.send').on('submit', function()
{
    $(this).find('input[type="submit"]').prop("disabled", true);
});

And then the PHP code to process: 然后要处理的PHP代码:

if(isset($_POST['finish']))
{
    //Do things
}

As I said, the PHP don't process the form because JS disabled the button to prevent multiple submitions. 就像我说的那样,PHP不处理表单,因为JS禁用了该按钮以防止多次提交。 How to process PHP before disable the button? 禁用按钮前如何处理PHP? Thanks! 谢谢!

Since you are disabling the submit button it will not get send over to to server in the $_POST variable so your code doesn't work, as you have said. 由于您禁用了提交按钮,因此不会通过$_POST变量将其发送到服务器,因此您的代码将无法正常工作。

Another way to do what you are looking for is to create a hidden HTML input 做您正在寻找的另一种方法是创建一个隐藏的HTML输入

<input type="hidden" name="form">

Then when you check if the form is send, you will use 然后,当您检查表单是否已发送时,将使用

if(isset($_POST['form']))
{
    //Do things
}

You can solve it easily changing your submit button by a simple button: 您可以通过一个简单的按钮轻松更改提交按钮来解决:

<form class='send' action='{$_SERVER['PHP_SELF']}' method='post'>
    <!--Form data-->
    <input type='button' name='finish' value='Send'/>
</form>

$('input[name="finish"]', '.send').on('click', function(){
    $(this).prop('disabled', true);
    $('.send').submit();
});

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

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