Determining Form Submission in PHP
When validating form submissions, it is essential to first ascertain if the form has actually been submitted. Traditionally, some may consider using isset($_POST) to check this. However, since superglobals are always defined, this approach returns true regardless.
Iterating through each form element with individual isset checks is tedious. To address this issue, one solution is to incorporate a hidden field as a flag.
Is there an alternative to adding a custom flag?
For a general check of POST actions, the following snippet can be employed:
if ($_POST)
Note: This method may not function reliably with certain form elements such as checkboxes.
Recommended Approach:
To enhance reliability, it is advisable to use the $_SERVER superglobal:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
The above is the detailed content of How Can I Reliably Determine if a Form Has Been Submitted in PHP?. For more information, please follow other related articles on the PHP Chinese website!