Use PHP to develop form verification and submission processing of small programs

王林
Release: 2023-07-05 16:48:02
Original
986 people have browsed it

Form verification and submission processing using PHP to develop small programs

When developing small programs in PHP, form verification and submission processing are a very important link. Properly validating user input and processing submitted data is key to ensuring program functionality and data security. This article will introduce how to use PHP for form validation and submission processing, with code examples.

1. Form verification
Form verification is to ensure that the data entered by the user meets the specifications and performs necessary verification before submission. Common form validation rules include: required fields cannot be empty, field length restrictions, numerical range restrictions, email format verification, mobile phone number format verification, etc.

The following is a sample code that uses PHP to implement form validation:

<?php
// 定义错误数组
$errors = [];

// 判断表单是否提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 验证姓名
    if (empty($_POST["name"])) {
        $errors["name"] = "姓名不能为空!";
    } else {
        $name = $_POST["name"];
    }

    // 验证邮箱
    if (empty($_POST["email"])) {
        $errors["email"] = "邮箱不能为空!";
    } elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
        $errors["email"] = "邮箱格式不正确!";
    } else {
        $email = $_POST["email"];
    }

    // 验证手机号
    if (empty($_POST["phone"])) {
        $errors["phone"] = "手机号不能为空!";
    } elseif (!preg_match("/^1[34578]d{9}$/", $_POST["phone"])) {
        $errors["phone"] = "手机号格式不正确!";
    } else {
        $phone = $_POST["phone"];
    }
}

?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <div>
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name" value="<?php echo $name ?? ''; ?>">
        <?php echo $errors["name"] ?? ''; ?>
    </div>
    <div>
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email" value="<?php echo $email ?? ''; ?>">
        <?php echo $errors["email"] ?? ''; ?>
    </div>
    <div>
        <label for="phone">手机号:</label>
        <input type="tel" id="phone" name="phone" value="<?php echo $phone ?? ''; ?>">
        <?php echo $errors["phone"] ?? ''; ?>
    </div>
    <button type="submit">提交</button>
</form>
Copy after login

In the above example, an error array $errors is first defined to store error information during the validation process. Then, after the form is submitted ($_SERVER["REQUEST_METHOD"] == "POST"), field-by-field verification is performed through if conditions. The validation rules for each field can be customized according to actual needs. Examples include validation rules for name, email, and mobile phone number. When verification fails, the error information is stored in the $errors array for subsequent display in the form.

2. Submission processing
After the form verification is passed, we need to process the data submitted by the user, such as storing it in the database or sending emails. In order to be safe and reliable, we need to filter and escape user input to prevent SQL injection and cross-site scripting attacks.

The following is a sample code using PHP for submission processing:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];

    // 对数据进行过滤和转义
    $name = filter_var($name, FILTER_SANITIZE_STRING);
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
    $phone = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

    // 进行数据库存储或其他操作
    // ...

    // 跳转到成功页面
    header("Location: success.php");
    exit;
}
?>
Copy after login

In the above code, first obtain the data submitted by the user, and use the filter_var function to filter and escape the data to Keep your data safe. Database storage or other required operations can then be performed. Finally, use the header function to jump to the page and direct the user to the success page.

Summary:
In PHP development applet, form verification and submission processing are a link that cannot be ignored. Through reasonable form validation and data processing, the normal operation of the program and the security of the data can be guaranteed. This article introduces the method of using PHP for form verification and submission processing through code examples. I hope it will be helpful to developers who are developing small programs.

The above is the detailed content of Use PHP to develop form verification and submission processing of small programs. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template