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
Release: 2024-12-05 20:59:11
Original
412 people have browsed it

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)
Copy after login

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')
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template