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>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;
?>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.";
}
?>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.";
}
?>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.";
}
?>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.";
}
?>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.