检查表单是否已提交 - PHP [英] Checking if form has been submitted - PHP
问题描述
检查表单是否被提交以确定我是否应该将表单的变量传递给我的验证类的最好方法是什么?
首先我想也许:
$ $ p $ isset($ _ POST)
但是,随着超全球化被定义在任何地方,这将永远回报真实。
if(isset($ _ POST ['element1'))我不希望迭代表单中的每个元素。 '])|| isset($ _ POST ['element2'])|| isset(... etc
<在写这个问题时,我想到了一个更基本的解决方案,添加一个隐藏字段来充当我可以检查的标志。
是否有'清洁'的方式来做它比添加我自己的国旗?
通常检查是否有一个 POST
操作使用:
if(!empty($ _ POST))
编辑:如评论所述,此方法不适用于在某些情况下(例如使用复选框和没有名字的按钮),你应该使用:
if($ _SERVER [ REQUEST_METHOD'] =='POST')
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:
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?
For general check if there was a POST
action use:
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')
这篇关于检查表单是否已提交 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!