Comprehensive PHP & MySQL Web Form This project is a complete, full-stack web application that demonstrates how to build a dynamic and secure web form. It serves as a robust template for creating data-driven websites, featuring a comprehensive front-end form, a secure PHP back-end for processing, and a MySQL database for storage.
Features
-
Comprehensive HTML5 Form: Includes a wide variety of input fields, from text and email to date/time pickers, color selectors, and file uploads.
-
Clean, Modern Design: Styled with CSS for a professional and responsive user interface that looks great on any device.
-
Secure Back-End Processing: The PHP back-end uses prepared statements to prevent SQL injection attacks and securely hashes user passwords.
-
Data Sanitization: Implements server-side sanitization, such as cleaning phone number inputs for consistent data storage.
-
File Upload Handling: Includes a script to manage and store uploaded files in a designated server directory.
-
Modular Code: Each form field is self-contained in its own
<div>, making it incredibly easy to copy and reuse individual parts in other projects. -
Data Display: A separate page fetches and displays all submitted records from the database in a clean, readable table.
Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.
You will need a local server environment that supports PHP and MySQL. The most common options are:
-
XAMPP (for Windows, macOS, and Linux)
-
WAMP (for Windows)
-
MAMP (for macOS)
-
Download the Project: Download all the project files (
index.php,style.css,submit.php,display.php, anddatabase.sql) and place them into a single folder (e.g.,simple-web-form). -
Move to Server Directory: Move this project folder into your local server's root directory. This is typically named
htdocsinXAMPPorwwwinWAMP/MAMP. -
Create the Database:
-
Start your local server's Apache and MySQL services.
-
Open your web browser and navigate to
http://localhost/phpmyadmin. -
Click on the "Import" tab.
-
Click "Choose File" and select the
database.sqlfile from the project folder. -
Click "Go" at the bottom of the page. This will create the
form_dbdatabase and theuserstable automatically.
-
-
Configure Database Connection:
-
Open both
submit.phpanddisplay.phpin a code editor. -
At the top of each file, update the database credentials to match your local server's setup. For most default XAMPP/WAMP installations, you only need to set the password (which is often blank).
-
$servername = "localhost";
$username = "root";
$password = ""; // <-- Change this if you have a password
$dbname = "form_db";- You're Ready! Open your web browser and navigate to
http://localhost/simple-web-form/index.php. You should now see the form and be able to use the application.
During the development of this project, we encountered a common usability challenge:
-
The Challenge: Initially, the phone number field used a strict HTML
patternattribute (pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}") to force users to enter dashes. This is poor practice, as users prefer to enter numbers in various formats (e.g., with spaces, parentheses, or no separators at all). -
The Solution: We improved the user experience by removing the restrictive
patternfrom the HTML. To ensure data consistency, we then implemented a server-side solution insubmit.php. Using a regular expression (preg_replace('/[^0-9]/', '', $phone_raw)), the back-end now automatically strips all non-numeric characters from the phone number before saving it to the database. This approach offers the best of both worlds: a flexible front-end for the user and clean, uniform data on the back-end.
One of the key design goals of this project was to make it easily reusable. The index.php file was built with modularity in mind.
Want to use just the color picker in another project? Simply copy its entire
<!-- 7. Color Picker -->
<div id="color-input-group" class="form-group">
<label for="favColor">Favorite Color (color):</label>
<input type="color" id="favColor" name="favColor" value="#3498db">
</div>
This self-contained structure applies to every single input field, allowing you to quickly assemble custom forms by picking and choosing the components you need.