Learn PHP Programming

A backend server scripting language, and a powerful tool for making dynamic and interactive Web pages. Learn each topic with examples and output.

PHP Introduction
Control Structures
Php Forms
Php Advanced

PHP Form Validation

Form validation in PHP is the process of ensuring that user-submitted data in HTML forms meets certain criteria or constraints before it is processed or stored in a database. The primary goals of form validation are to:

Here's a basic example of form validation in PHP to validate a user's registration form:

<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>
    <h2>Registration Form</h2>
    <form method="post" action="processregistration.php">
 <label for="username">Username:</label>
 <input type="text" id="username" name="username" required><br><br>

 <label for="email">Email:</label>
 <input type="email" id="email" name="email" required><br><br>

 <label for="password">Password:</label>
 <input type="password" id="password" name="password" required><br><br>

 <label for="confirm_password">Confirm Password:</label>
 <input type="password" id="confirm_password" name="confirm_password" required>
 <br><br>

<input type="submit" value="Register">
    </form>
</body>
</html>

In this HTML code, we have a simple registration form with fields for username, email, password, and password confirmation. The form sends the data to a PHP script named processregistration.php for validation.

Now, let's create the processregistration.php script to handle form validation:

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $username = $_POST["username"];
        $email = $_POST["email"];
        $password = $_POST["password"];
        $confirm_password = $_POST["confirm_password"];
        
        // Basic validation checks
        $errors = [];
        
        if (strlen($username) < 3) {
            $errors[] = "Username must be at least 3 characters long.";
        }
        
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = "Invalid email address.";
        }
        
        if (strlen($password) < 6) {
            $errors[] = "Password must be at least 6 characters long.";
        }
        
        if ($password !== $confirm_password) {
            $errors[] = "Passwords do not match.";
        }
        
        if (empty($errors)) {
            // Process the registration (e.g., insert data into a database)
            // Redirect to a success page
            header("Location: validform.html");
        } else {
            // Display errors to the user
            foreach ($errors as $error) {
                echo $error . "
"; } } } ?>

In this PHP code:

We check if the request method is POST, indicating that the form has been submitted.

We retrieve the values of the username, email, password, and confirm_password fields from the $_POST superglobal.

We perform basic validation checks on the data:

  1. Username must be at least 3 characters long.
  2. Email must be a valid email address.
  3. Password must be at least 6 characters long.
  4. The password and confirm_password fields must match.
  5. If there are no validation errors, the script can proceed to process the registration (e.g., inserting data into a database) and then redirect the user to a success page.

If there are validation errors, we display them to the user so they can correct their input.

This example demonstrates a basic form validation process in PHP. Depending on the complexity of your application, you may need to implement more robust validation and security measures, such as database sanitization, CAPTCHA verification, and input filtering to protect against various types of attacks and ensure data integrity.

In next chapter we start learning php form Required attribute


Share this page on :

=