Build a PHP Contact Form
Learn how to build a complete PHP Contact Form that receives user information, validates the data and stores it inside a MySQL database.
What Are We Building?
In this project, we will create a professional contact form using HTML, CSS, PHP and MySQL.
The user will enter their name, email, phone, subject and message. PHP will process the submitted information and save it into MySQL.
What You Will Learn
Requirements
Step 1 — Create the Project Folder
Go to your AIFA Coding Lab PHP projects folder and create a folder named:
contact-form
Inside it, create these files:
contact-form/ │ ├── tutorial.php ├── index.php ├── submit.php └── style.css
Step 2 — Create the MySQL Database
Open phpMyAdmin and create a database named:
aifa_php_lab
Then create a table named:
contact_messages
The table should contain these columns:
id name email phone subject message created_at
The SQL used in this project is:
CREATE TABLE contact_messages (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL,
phone VARCHAR(20) NOT NULL,
subject VARCHAR(200) NOT NULL,
message TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 3 — Create the HTML Form
Open index.php. The form collects five pieces of information:
Name Email Phone Subject Message
The important part of the form is:
<form action="submit.php" method="POST">
<input type="text" name="name">
<input type="email" name="email">
<input type="tel" name="phone">
<input type="text" name="subject">
<textarea name="message"></textarea>
<button type="submit">
Send Message
</button>
</form>
The action tells the browser where to send the form data.
The POST method sends the data to the server without placing it in the URL.
Step 4 — Receive Form Data Using PHP
In submit.php, PHP receives the submitted information using the $_POST superglobal.
$name = trim($_POST['name'] ?? ''); $email = trim($_POST['email'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $subject = trim($_POST['subject'] ?? ''); $message = trim($_POST['message'] ?? '');
The trim() function removes unnecessary spaces from the beginning and end of the submitted value.
Step 5 — Validate the Form
Before storing data, we should check whether the user entered valid information.
if (
$name === '' ||
$email === '' ||
$phone === '' ||
$subject === '' ||
$message === ''
) {
// Show error
}
We also check the email address:
if (!filter_var(
$email,
FILTER_VALIDATE_EMAIL
)) {
// Invalid email
}
Step 6 — Connect PHP With MySQL
Our XAMPP MySQL server is running on port 3307.
$host = "127.0.0.1";
$user = "root";
$password = "";
$database = "aifa_php_lab";
$port = 3307;
$conn = new mysqli(
$host,
$user,
$password,
$database,
$port
);
The mysqli class creates the connection between PHP and MySQL.
Step 7 — Insert Data Into MySQL
We use a prepared statement to insert the submitted information.
$sql = "
INSERT INTO contact_messages
(name, email, phone, subject, message)
VALUES (?, ?, ?, ?, ?)
";
$stmt = $conn->prepare($sql);
$stmt->bind_param(
"sssss",
$name,
$email,
$phone,
$subject,
$message
);
$stmt->execute();
The five s characters represent five string values.
Step 8 — Understand the Complete Flow
Important Points
Practice Questions
Ready to See the Complete Project?
You have learned how the project works. Now open the working project.
🚀 Run Complete Project