Home>Article>Backend Development> PHP form learning: How to verify the required fields in the form?

PHP form learning: How to verify the required fields in the form?

WBOY
WBOY Original
2021-10-29 11:51:00 1450browse

In the previous article, I brought you "PHP Form Learning: Form Input and Verification", which introduced the relevant knowledge of form input and verification in PHP in detail. In this article, we Let's continue to see how to validate the required fields in the form. I hope everyone has to help!

PHP form learning: How to verify the required fields in the form?

In the previous article, we learned about input in the form and simple validation of the form through PHP. When we enter information, we will not be prompted even if our information is not entered. In our daily life, when we need to fill in information, we must fill in some important information. If we do not fill it in, there will be a prompt message telling us where the information has not been filled in.

This is actually what we call a required field, so how is the effect that such a required field is required to be achieved? Then let's take a look at how to get the required fields and error messages in the form.

PHP required fields

We have encountered required fields when we fill in the information during daily registration. The required fields are where we fill in the information. time, it must be filled in, otherwise there will be no way to pass.

In the previous article, we have introduced the validation rules of the form. In the example we gave, we introduced: the validation rules of the name are necessary, and the validation rules of the E-mail are necessary. Among them, they must be A valid email address (containing '@' and '.'). A gender validation rule is required and one must be selected. This is a required field in our power.

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

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

These error variables will be displayed on required fields. We also added anif elsestatement to each $_POST variable. These statements will check if the $_POST variable is empty (using PHP'sempty()function). If it is empty, the corresponding error message will be displayed. If not empty, the data will be passed to thetest_input()function.

Next let’s take a look at the actual application of these variables:

The output results are currently not visible. This code needs to verify the form data through the form code.

The name verification rule is required, and the E-mail verification rule is required, which must be a valid email address (including '@' and '.'). A gender validation rule is required and one must be selected.

We are not able to display the error through the above code. If we submit the above code knowledge without writing anything in the three required fields input boxes of name, email and gender, it will be displayed on the page. error message. If it is not blank, it will be verified, and then the content in the lse statement will be executed. Then let's take a look at how to display the error information.

In the HTML example form, we added some scripts to each field. Each script will display an error message when the information is entered incorrectly. In order to achieve the purpose of outputting an error message if the user submits the form without filling in the information.

Let’s take a look at how to display error information through an example. An example is as follows:

    PHP中文网 

PHP 验证实例

* 必需的字段

"> 姓名: *

电邮: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>

After passing the verification data, if we click submit without filling in our marked name, email and gender information, the output will be as follows:

PHP form learning: How to verify the required fields in the form?

From the above example, we have completed the filling requirements of the required fields in the PHP form through the variables $nameErr, $emailErr, $genderErr, and $websiteErr, as well as the ifelse statement and error display.

If you feel that the text reminder is not eye-catching enough, it is more common in our daily life that when you click submit, there will be a small pop-up window on the page. Next, as a way to expand knowledge, let’s take a look at how Implement pop-up reminders.

The example is as follows:

    PHP中文网 
//声明标识符 { alert(\"姓名是必填的\"); //弹出对话框 } "; } else { $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = ""; } else { $email = test_input($_POST["email"]); } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = ""; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>

PHP 验证实例

* 必需的字段

"> 姓名: *

电邮: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>

When we input the result normally, the output result is as follows:

PHP form learning: How to verify the required fields in the form?

When we do not output the result , the following pop-up window will appear on the page. If an item is not filled in, a pop-up window will remind you:

PHP form learning: How to verify the required fields in the form?

PHP form learning: How to verify the required fields in the form?

PHP form learning: How to verify the required fields in the form?

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of PHP form learning: How to verify the required fields in the form?. 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