Home >Backend Development >PHP Tutorial >How Can I Reliably Determine if a Form Has Been Submitted in PHP?

How Can I Reliably Determine if a Form Has Been Submitted in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-05 20:59:11408browse

How Can I Reliably Determine if a Form Has Been Submitted in PHP?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn