Checking if form has been submitted - PHP

2019-01-03 01:31发布

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?

8条回答
仙女界的扛把子
2楼-- · 2019-01-03 02:16

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

查看更多
一夜七次
3楼-- · 2019-01-03 02:20

You could also use:

is_array($_POST)
查看更多
登录 后发表回答