开发者

If HTML form was submtited check

开发者 https://www.devze.com 2023-04-08 09:19 出处:网络
is if ($_POST[\'submit\']) ... same as if (isset($_POST[\'submit\']) ... or it means something else? Which one i should use to check if开发者_开发百科 form was submitted?

is if ($_POST['submit']) ... same as if (isset($_POST['submit']) ... or it means something else? Which one i should use to check if开发者_开发百科 form was submitted?

Is there some way to pull form name?


Use if ($_SERVER['REQUEST_METHOD'] == 'POST'). All other methods are flawed. What if you rename the submit button?


To get the form name you can either:

1) use a hidden input field whose value is the same as the form name

2) set the name of the submit button to the form name

The actual form name is not sent in the POST or GET variables.


isset

Determines if a variable is set and is not NULL

Boolean cast (same as !empty($var))

empty returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* 0.0 (0 as a float)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

Pages can have more than one form. It's best to give your submit element a value as well:

<input type="hidden" name="submit" value="login" />

Then in your PHP code, you can:

if (isset($_POST['submit']) && $_POST['submit'] == 'login') {

}


if(isset($_POST['submit'])) checks if the variable exists or in this case if the key exists under the array and is not null.

if($_POST['submit']) will check if the value of $_POST['submit'] is equal to true (or some other value that evaluates to true).

the second method there, will throw a notice error if the variable is not set. You should use isset to check if a variable is set, that's what it is for.


they are the same but using if(isset($_POST['submit']) or if(!empty($_POST['submit']) are better

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号