How to Process a Simple Form with PHP?

Discussion forum for web development. Covers frontend and backend technologies including HTML, CSS, JavaScript, PHP and modern frameworks. Topics include websites, web applications, APIs, UI/UX, performance, debugging and server-side development. Suitable for beginners and experienced developers.
Post Reply
MegaTux
Posts: 34
Joined: Thu Apr 16, 2026 6:21 am

How to Process a Simple Form with PHP?

Post by MegaTux »

One of the first practical things beginners learn in PHP is how to process a simple HTML form. Forms are used everywhere on websites. They can collect names, email addresses, messages, search terms, login data, and much more. PHP is often used on the server to receive that form data and work with it.

The basic idea is simple: the user fills out a form in the browser, clicks submit, and the browser sends the data to a PHP file. That PHP file can then read the values and display them, save them, validate them, or send them by email.

Step 1: Create a Simple HTML Form

Here is a basic example:

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple PHP Form</title>
</head>
<body>

<h2>Contact Form</h2>

<form action="process.php" method="post">
    <label for="name">Name:</label><br>
    <input type="text" name="name" id="name"><br><br>

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

    <button type="submit">Send</button>
</form>

</body>
</html>
This form sends the data to a file called process.php using the POST method.

Step 2: Read the Form Data in PHP

Now create the file process.php:

Code: Select all

<?php

$name = $_POST['name'];
$email = $_POST['email'];

echo "Hello, " . $name . "!<br>";
echo "Your email address is: " . $email;

?>
When the form is submitted, PHP reads the values from $_POST and prints them on the page.

Step 3: Check if the Form Was Submitted

A better version checks whether the form was actually submitted before trying to use the values:

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];

    echo "Hello, " . $name . "!<br>";
    echo "Your email address is: " . $email;
} else {
    echo "No form data received.";
}

?>
This helps avoid warnings if someone opens the PHP file directly without submitting the form first.

Step 4: Make It Safer

When working with user input, it is important not to trust it blindly. A user can type anything into a form. To safely display data in HTML, use htmlspecialchars():

Code: Select all

<?php

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

    echo "Hello, " . $name . "!<br>";
    echo "Your email address is: " . $email;
} else {
    echo "No form data received.";
}

?>
This prevents special HTML characters from being interpreted as code in the browser.

Step 5: Add Basic Validation


It is also a good idea to check whether the fields are empty:

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = htmlspecialchars(trim($_POST['name']));
    $email = htmlspecialchars(trim($_POST['email']));

    if (empty($name) || empty($email)) {
        echo "Please fill in all fields.";
    } else {
        echo "Hello, " . $name . "!<br>";
        echo "Your email address is: " . $email;
    }
} else {
    echo "No form data received.";
}
?>
Here, trim() removes extra spaces at the beginning and end of the input.

Step 6: Validate the Email Properly

For email addresses, PHP has a built-in filter:

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = htmlspecialchars(trim($_POST['name']));
    $email = trim($_POST['email']);

    if (empty($name) || empty($email)) {
        echo "Please fill in all fields.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Please enter a valid email address.";
    } else {
        $safe_email = htmlspecialchars($email);

        echo "Hello, " . $name . "!<br>";
        echo "Your email address is: " . $safe_email;
    }
} else {
    echo "No form data received.";
}

?>
This is a much better approach than only checking if the field “looks right”.

Why This Matters

Processing forms is one of the foundations of PHP web development. Once you understand how to:

create a form
send it with POST
read values with $_POST
validate input
safely output user data

you already understand an important part of how dynamic websites work.
Post Reply