Form handling execution flow shows how a web page collects user input and processes it step-by-step.
Form handling execution flow in 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.
<?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>
<?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>
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 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>
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.
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.