-
Hajipur, Bihar, 844101
Forms are an essential part of web development. They allow users to submit information, such as contact messages, registration details, or feedback, which can then be processed and stored using PHP. Proper form handling ensures that data is collected securely and efficiently.
When a form is submitted, the data is sent to the server using either:
GET method – Data appears in the URL; suitable for non-sensitive information.
POST method – Data is sent in the HTTP request body; preferred for sensitive information like passwords.
PHP accesses form data using superglobals:
$_GET
→ For data sent via GET method
$_POST
→ For data sent via POST method
$_REQUEST
→ Can handle both GET and POST data
A contact form usually includes Name, Email, and Message fields.
<!-- contact.html -->
<form action="contact_process.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Message: <textarea name="message"></textarea><br>
<input type="submit" value="Send">
</form>
<!-- contact_process.php -->
<?php
// Accessing submitted data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Displaying the submitted information
echo "Hello $name,<br>";
echo "Your email: $email<br>";
echo "Your message: $message<br>";
?>
Explanation:
The form uses method="post"
to send data securely.
$_POST['name']
retrieves the Name field value.
Email and Message fields are handled similarly.
This is basic form handling without validation or sanitization.
A registration form typically collects Name, Email, Password, and Gender.
<!-- register.html -->
<form action="register_process.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
Gender:
<input type="radio" name="gender" value="Female"> Female
<input type="radio" name="gender" value="Other"> Other<br>
<input type="submit" value="Register">
</form>
<!-- register_process.php -->
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
// Displaying registration information
echo "Hello $name!<br>";
echo "Email: $email<br>";
echo "Gender: $gender<br>";
// Note: Password should be hashed in real applications
?>
Explanation:
The form sends user data to register_process.php
.
Data is accessed using $_POST['field_name']
.
Passwords should always be hashed using password_hash()
before storage.
This example shows Vicky, Sanjana, and Vrinda as sample users.
To prevent malicious input, always sanitize user input using htmlspecialchars()
:
<?php
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
echo "Hello $name, your message: $message has been received.";
?>
Explanation:
Converts HTML special characters to safe entities.
Protects against cross-site scripting (XSS).
When validation fails, it’s user-friendly to retain previously entered values:
<input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>">
Explanation:
Checks if a value exists and keeps it in the input field.
Makes forms easier to correct without re-entering all data.
<?php
$names = ["Vicky", "Sanjana", "Vrinda", "Aisha", "Neha"];
foreach ($names as $name) {
echo "Welcome, $name!<br>";
}
?>
Explanation:
Iterates over an array of names.
Displays a greeting for each girl, demonstrating dynamic form handling.
Sometimes, you may want to process arrays submitted via forms:
<?php
// Form field: <input type="checkbox" name="interests[]" value="Music">
$interests = $_POST['interests']; // Array of selected interests
foreach ($interests as $interest) {
echo "You selected: $interest<br>";
}
?>
Explanation:
Handles multiple inputs like checkboxes or multi-select fields.
Loops through the array to process each selected value.
Use POST method for sensitive data.
Validate all input to prevent invalid or malicious data.
Escape output using htmlspecialchars()
.
Hash passwords before saving in a database.
Keep forms user-friendly by retaining input values.
Use descriptive names for form fields for easy processing.
Forms collect data like messages or registration details from users.
PHP accesses form data using $_POST (preferred) or $_GET.
Contact forms handle Name, Email, and Message, while registration forms may include Password and Gender.
Always sanitize and validate user input.
Loops and arrays can process multiple users or multiple form inputs efficiently.
Mastering PHP form handling is essential for creating secure, interactive, and dynamic web applications.
Create a contact form with Name, Email, and Message fields, then display the submitted data using PHP.
Create a registration form with Name, Email, Password, and Gender fields, then display the submitted data.
Modify the contact form to retain the previously entered values after submission.
Validate the registration form to ensure Name and Email are not empty.
Validate the Email field in the registration form using filter_var()
and display an error if invalid.
Sanitize all input in the contact form using htmlspecialchars()
before displaying it.
Create a form with multiple checkbox options for interests and display all selected interests after submission.
Create a form with a dropdown for selecting a favorite subject and display the selected value.
Loop through an array of girls’ names (Vicky, Sanjana, Vrinda, Aisha, Neha) and display a greeting message for each.
Create a registration form that checks if the password field is empty and displays a message if it is.