Get the App
SLTechnology News&Howtos  ›  Development  › 

How to validate the required fields in a form in PHP

Shulou Source: shulou.com Published: 2022-06-02 20:57:21 09月13日 Update

This article mainly shows you "how to verify the necessary fields in the form in PHP", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to verify the necessary fields in the form in PHP" this article.

PHP required fields

When we have to fill in the information in our daily registration, we have encountered the necessary fields, and the necessary fields must be filled in when we fill in the information, otherwise there is no way to pass.

In the previous article, we have introduced the validation rules of the form, in which we introduced in the example: name validation rules are required, E-mail validation rules are required, which must be a valid e-mail address (including'@ 'and'.'). Gender verification rules are necessary and one must be selected. This is the necessary field that belongs to our power.

In the example in the previous article, all the input fields are optional because we did not validate them. So how can we achieve the effect that required fields are required?

At this point we can add some new variables to the following code: $nameErr, $emailErr, $genderErr, and $websiteErr.

These error variables are displayed on the required fields. We also added an if else statement for each $_ POST variable. These statements check to see if the $_ POST variable is empty (using php's empty () function). If empty, the corresponding error message is displayed. If it is not empty, the data is passed to the test_input () function.

Let's take a look at the practical application of these variables:

The output is not currently visible, and this code needs to validate the form's data through the form code.

Name validation rules are required, and E-mail validation rules are required, which must be a valid e-mail address (including'@ 'and'.'). Gender verification rules are necessary and one must be selected.

Through the above code, we have not been able to show the error. If we submit the above code knowledge without writing anything in the name, mailbox and gender input boxes, the error message will be displayed on the page. If it is not blank, it will be verified, and then execute the contents of the lse statement. Then let's take a look at how to display the wrong message.

In the HTML instance form of, we add some scripts to each field, and each script displays an error message when the information is entered incorrectly. So that if the user submits the form without filling in the information, the error message will be output.

Let's take a look at how to display the wrong message through an example. Examples are as follows:

.error {color: # FF0000;} PHP verification instance

* required fields

Tags: Validation fields forms errors information rules variables code gender content examples statements input name data article output valid next function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno vpn Apple Microsoft