How to create perfect form validation with PHP

王林
Release: 2023-06-27 15:56:01
Original
1281 people have browsed it

In web development, form validation is a very important part. It can ensure that the user's input data conforms to the specified format and rules, effectively preventing some unnecessary errors and malicious behaviors. As a powerful and popular programming language, PHP can implement form validation functions by writing code. But, how to create perfect form validation with PHP? This article will introduce you in detail from the following aspects.

1. Call form submission and verification

To use PHP to implement form verification, you must first call the submit button in the HTML form. In an HTML form, you can use the <form> tag to include form elements, such as <input>, <textarea>, etc. In the PHP code, you need to specify the request method corresponding to the form submission, that is, $_REQUEST or $_POST.

For example, in an HTML form, suppose we define a text box for entering a user name. Then, its submission and verification can be called like this in PHP code:

<?php
if(isset($_REQUEST['submit'])){
$username = $_POST['username'];
//进行验证
}
?>
Copy after login

Here, the isset() function can check whether the request exists and return true or false. The $_POST refers to the form data submitted through the POST request.

2. Verify form input data

When validating a form, you usually need to check whether the user's input data conforms to the specified format and rules. For example, for the entered username, we can check whether its length exceeds the limit, whether it contains illegal characters, etc. Common form verification methods are as follows:

1. Check the input format: Verify whether the format of the input data complies with the protocol. For example, when validating an email address, you can use regular expressions to check whether it is formatted correctly.

if(preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/',$_POST['email'])){
//合法
}
Copy after login

2. Check the string length: Verify whether the length of the input string meets the requirements. For example, when validating a username, you can check if its length is between 6-20 characters.

if( strlen($username) >= 6 && strlen($username) <= 20 ){
//合法
}
Copy after login

3. Check whether it is empty: Verify whether the required fields are entered, and detect whether the data is empty. For example, when validating a password, you can check to see if it is empty.

if(empty($_REQUEST['password'])){
//密码为空,需要填写
}
Copy after login

3. Processing form submission data

After verification, the input data of the form already conforms to the specified rules and formats, but we still need to process the data, such as saving it to the database or send an email, etc. In PHP code, you can use the $_REQUEST and $_POST global variables to get the form submission information and then process it. For example:

<?php
if( isset($_REQUEST['submit']) ){
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);

//将数据保存到数据库中
mysqli_query($db,"INSERT INTO user (username,email,password) VALUES ('$username','$email','$password')");

//发送电子邮件
$to = $email;
$subject = '注册成功';
$message = '恭喜您!您已经成功注册本站账户。';
$headers = 'From: webmaster@example.com';
mail($to,$subject,$message,$headers);
}
?>
Copy after login

4. Handling error information

When performing form verification, some errors often occur, such as the input data format is incorrect, required fields are empty, etc. In response to these error situations, we need to provide timely feedback to users and prompt correct operation steps. In PHP code, you can use the echo statement and the die() function to output error information. The example is as follows:

if(preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/',$_POST['email'])){
//合法
}else{
echo '输入的邮箱地址不正确,请重新输入!';
die();
}
Copy after login

5. Conclusion

PHP form validation is a very important part of web development. During the implementation process, user experience, security, and code readability need to be fully taken into consideration. By introducing the above aspects in detail, I believe you can already master how to use PHP to create perfect form validation.

The above is the detailed content of How to create perfect form validation with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!