In this section, we will learn how to handle form data in PHP. Forms are a fundamental part of web applications, allowing users to submit data that can be processed and stored by the server. We will cover the following topics:

  1. Creating a Simple HTML Form
  2. Using the $_GET and $_POST Superglobals
  3. Processing Form Data
  4. Validating Form Data
  5. Practical Example

  1. Creating a Simple HTML Form

To handle form data in PHP, we first need to create an HTML form. Here is a simple example of an HTML form that collects a user's name and email address:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Form</title>
</head>
<body>
    <form action="process_form.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>
        <br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Explanation:

  • The action attribute specifies the URL of the PHP script that will process the form data.
  • The method attribute specifies the HTTP method to use when sending form data (post in this case).
  • The name attribute of each input element is used to identify the form data when it is submitted.

  1. Using the $_GET and $_POST Superglobals

PHP provides two superglobal arrays, $_GET and $_POST, to collect form data sent via the GET and POST methods, respectively.

  • $_GET: Used to collect form data after submitting an HTML form with the method="get".
  • $_POST: Used to collect form data after submitting an HTML form with the method="post".

  1. Processing Form Data

Let's create a PHP script (process_form.php) to process the form data submitted via the POST method:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect and sanitize form data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);

    // Display the collected data
    echo "Name: " . $name . "<br>";
    echo "Email: " . $email . "<br>";
}
?>

Explanation:

  • $_SERVER["REQUEST_METHOD"] checks the request method used to access the page.
  • htmlspecialchars() function converts special characters to HTML entities to prevent XSS attacks.

  1. Validating Form Data

Validation ensures that the data submitted by the user meets certain criteria. Here is an example of how to validate the form data:

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

    // Validate name
    if (empty($name)) {
        echo "Name is required.<br>";
    } else {
        echo "Name: " . $name . "<br>";
    }

    // Validate email
    if (empty($email)) {
        echo "Email is required.<br>";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format.<br>";
    } else {
        echo "Email: " . $email . "<br>";
    }
}
?>

Explanation:

  • empty() function checks if a variable is empty.
  • filter_var() function with FILTER_VALIDATE_EMAIL validates the email format.

  1. Practical Example

Let's combine everything into a complete example:

HTML Form (index.html):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Handling Example</title>
</head>
<body>
    <form action="process_form.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>
        <br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

PHP Script (process_form.php):

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

    // Validate name
    if (empty($name)) {
        echo "Name is required.<br>";
    } else {
        echo "Name: " . $name . "<br>";
    }

    // Validate email
    if (empty($email)) {
        echo "Email is required.<br>";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format.<br>";
    } else {
        echo "Email: " . $email . "<br>";
    }
}
?>

Conclusion

In this section, we learned how to handle form data in PHP. We covered creating a simple HTML form, using the $_GET and $_POST superglobals, processing form data, and validating form data. Handling form data is a crucial skill for any PHP developer, as it forms the basis of user interaction in web applications. In the next section, we will delve deeper into form validation to ensure the data we collect is accurate and secure.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved