检查表单是否已提交 - PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7711466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:13:27  来源:igfitidea点击:

Checking if form has been submitted - PHP

phpformsvalidation

提问by Anonymous

What is the best way of checking whether or not a form has been submitted to determine whether I should pass the form's variables to my validation class?

检查表单是否已提交以确定是否应将表单变量传递给验证类的最佳方法是什么?

First I thought maybe:

首先我想也许:

isset($_POST)

But that will always return true as a superglobal is defined everywhere. I don't want to have to iterate through each element of my form with:

但这将始终返回 true,因为在任何地方都定义了超全局。我不想遍历表单的每个元素:

if(isset($_POST['element1']) || isset($_POST['element2']) || isset(...etc

Whilst writing this question I thought of a much more basic solution, add a hidden field to act as a flag that I can check.

在写这个问题时,我想到了一个更基本的解决方案,添加一个隐藏字段作为我可以检查的标志。

Is there a 'cleaner' way to do it than adding my own flag?

有没有比添加我自己的标志更“干净”的方法?

回答by matino

For general check if there was a POSTaction use:

对于一般检查是否有POST动作使用:

if (!empty($_POST))

EDIT: As stated in the comments, this method won't work for in some cases (e.g. with check boxes and button without a name). You really should use:

编辑:如评论中所述,此方法在某些情况下不起作用(例如,带有复选框和没有名称的按钮)。你真的应该使用:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

回答by Olaf

How about

怎么样

if($_SERVER['REQUEST_METHOD'] == 'POST')

回答by Tzshand

Actually, the submit button already performs this function.

实际上,提交按钮已经执行了这个功能。

Try in the FORM:

在表格中尝试:

<form method="post">
<input type="submit" name="treasure" value="go!">
</form>

Then in the PHP handler:

然后在 PHP 处理程序中:

if (isset($_POST['treasure'])){
echo "treasure will be set if the form has been submitted (to TRUE, I believe)";
}

回答by Rikesh

Use

if(isset($_POST['submit'])) // name of your submit button

回答by CodeCaster

if ($_SERVER['REQUEST_METHOD'] == 'POST').

if ($_SERVER['REQUEST_METHOD'] == 'POST').

回答by rizon

Try this

尝试这个

 <form action="" method="POST" id="formaddtask">
      Add Task: <input type="text"name="newtaskname" />
      <input type="submit" value="Submit"/>
 </form>

    //Check if the form is submitted
    if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['newtaskname'])){

    }

回答by Jen

I had the same problem - also make sure you add name=""in the input button. Well, that fix worked for me.

我遇到了同样的问题 - 还要确保您添加name=""了输入按钮。嗯,这个修复对我有用。

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['add'])){
    echo "stuff is happening now";
}

<input type="submit" name="add" value="Submit">

回答by Professor

On a different note, it is also always a good practice to add a token to your form and verify it to check if the data was not sent from outside. Here are the steps:

另一方面,在表单中添加令牌并验证它以检查数据是否不是从外部发送的,这也是一个好习惯。以下是步骤:

  1. Generate a unique token (you can use hash) Ex:

    $token = hash (string $algo , string $data [, bool $raw_output = FALSE ] );
    
  2. Assign this token to a session variable. Ex:

    $_SESSION['form_token'] = $token;
    
  3. Add a hidden input to submit the token. Ex:

    input type="hidden" name="token" value="{$token}"
    
  4. then as part of your validation, check if the submitted token matches the session var.

    Ex: if ( $_POST['token'] === $_SESSION['form_token'] ) ....
    
  1. 生成唯一的令牌(您可以使用哈希)例如:

    $token = hash (string $algo , string $data [, bool $raw_output = FALSE ] );
    
  2. 将此令牌分配给会话变量。前任:

    $_SESSION['form_token'] = $token;
    
  3. 添加隐藏输入以提交令牌。前任:

    input type="hidden" name="token" value="{$token}"
    
  4. 然后作为验证的一部分,检查提交的令牌是否与会话变量匹配。

    Ex: if ( $_POST['token'] === $_SESSION['form_token'] ) ....
    

回答by SW4

You could also use:

您还可以使用:

is_array($_POST)