PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Creating form elements in HTML and processing them with PHP is a common task in web development. Here’s a basic example to illustrate how you can create a form in HTML and handle the form data using PHP.

HTML Form

Here is an example of a simple HTML form:

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <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><br>

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

        <label for="message">Message:</label>
        <textarea id="message" name="message" required></textarea><br><br>

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

				
			

PHP Form Processing

Here’s how you can process the form data with PHP in a file named ‘process_form.php‘:

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

    // Display the collected data
    echo "<h1>Form Data</h1>";
    echo "<p>Name: $name</p>";
    echo "<p>Email: $email</p>";
    echo "<p>Message: $message</p>";

    // You can further process the data here (e.g., save to a database, send an email, etc.)
}
?>

				
			

Explanation

  • HTML Form:

    • The <form> element’s action attribute specifies the URL of the PHP file (process_form.php) that will handle the form submission.
    • The method attribute is set to post to send form data as a POST request.
    • Form fields include:
      • A text input for the name.
      • An email input for the email address.
      • A textarea for the message.
      • A submit button to send the form data.
  • PHP Form Processing:

    • The $_SERVER["REQUEST_METHOD"] is used to check if the form was submitted via POST.
    • htmlspecialchars() is used to sanitize the input data to prevent XSS (Cross-Site Scripting) attacks.
    • The form data is collected and then displayed on the page

Additional Elements

You can include other types of form elements such as radio buttons, checkboxes, dropdown menus, and more. Here’s an example with additional elements:

				
					<form action="process_form.php" method="post">
    <label for="gender">Gender:</label>
    <input type="radio" id="male" name="gender" value="male">
    <label for="male">Male</label>
    <input type="radio" id="female" name="gender" value="female">
    <label for="female">Female</label><br><br>

    <label for="hobbies">Hobbies:</label>
    <input type="checkbox" id="hobby1" name="hobbies[]" value="reading">
    <label for="hobby1">Reading</label>
    <input type="checkbox" id="hobby2" name="hobbies[]" value="traveling">
    <label for="hobby2">Traveling</label>
    <input type="checkbox" id="hobby3" name="hobbies[]" value="sports">
    <label for="hobby3">Sports</label><br><br>

    <label for="country">Country:</label>
    <select id="country" name="country">
        <option value="usa">USA</option>
        <option value="canada">Canada</option>
        <option value="uk">UK</option>
    </select><br><br>

    <input type="submit" value="Submit">
</form>

				
			

And in ‘process_form.php', you would handle these additional inputs as follows:

				
					<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);
    $gender = htmlspecialchars($_POST['gender']);
    $hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : [];
    $country = htmlspecialchars($_POST['country']);

    echo "<h1>Form Data</h1>";
    echo "<p>Name: $name</p>";
    echo "<p>Email: $email</p>";
    echo "<p>Message: $message</p>";
    echo "<p>Gender: $gender</p>";
    echo "<p>Hobbies: " . implode(", ", $hobbies) . "</p>";
    echo "<p>Country: $country</p>";
}
?>

				
			

This example demonstrates how to handle multiple types of inputs and process the form data in PHP.

Scroll to Top