0
0
PHPprogramming~5 mins

Form handling execution flow in PHP

Choose your learning style9 modes available
Introduction

Form handling execution flow shows how a web page collects user input and processes it step-by-step.

When you want to get user information like name or email from a web form.
When you need to check if the user filled the form correctly before saving data.
When you want to show a message after the user submits a form.
When you want to keep the form data visible after submission for correction.
When you want to send form data to a server for processing.
Syntax
PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Process form data here
    $name = $_POST['name'];
    // Validate and use $name
}
?>

<form method="post" action="">
    <input type="text" name="name">
    <input type="submit" value="Send">
</form>

The $_SERVER['REQUEST_METHOD'] checks if the form was submitted using POST.

The $_POST array holds the data sent by the form.

Examples
This example gets an email from the form and shows it after submission.
PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
    echo "Email received: $email";
}
?>

<form method="post" action="">
    <input type="email" name="email">
    <input type="submit" value="Submit">
</form>
This example checks if the age entered is a number before showing it.
PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $age = $_POST['age'];
    if (is_numeric($age)) {
        echo "Your age is $age.";
    } else {
        echo "Please enter a valid number.";
    }
}
?>

<form method="post" action="">
    <input type="text" name="age">
    <input type="submit" value="Check Age">
</form>
Sample Program

This program shows a form asking for a username. When submitted, it checks if the username is empty. If empty, it asks to enter it. Otherwise, it greets the user. The form keeps the entered username visible after submission.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    if (empty($username)) {
        $message = "Please enter your username.";
    } else {
        $message = "Hello, $username!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form Handling Flow</title>
</head>
<body>
    <form method="post" action="">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($_POST['username'] ?? '', ENT_QUOTES); ?>">
        <input type="submit" value="Submit">
    </form>
    <?php if (isset($message)) { echo "<p>$message</p>"; } ?>
</body>
</html>
OutputSuccess
Important Notes

Always check $_SERVER['REQUEST_METHOD'] to know if the form was submitted.

Use htmlspecialchars() to safely show user input in the form to avoid security issues.

Keep form data visible after submission to help users fix mistakes easily.

Summary

Form handling execution flow controls how data moves from user input to server processing.

Check the request method to know when to process form data.

Validate and keep user input visible for better user experience.