Day 8-9: PHP Form Handling
Handling form data is a crucial aspect of web development. In this section, we’ll learn how to create HTML forms and process the data submitted using PHP.
Creating an HTML Form
Let’s start by creating a simple HTML form to collect user data.
[mycode]
<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<form action=”process_form.php” method=”post”>
Name: <input type=”text” name=”name”><br>
Age: <input type=”text” name=”age”><br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
[/mycode]
action="process_form.php"
specifies the PHP script that will handle the form data.method="post"
specifies that the form data should be sent via POST method.
Processing Form Data with PHP
Create a file named process_form.php
to handle the form data.
[mycode]
<?php
// Check if form is submitted
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// Retrieve form data using the POST superglobal array
$name = $_POST[‘name’];
$age = $_POST[‘age’];
// Validate and sanitize inputs
if (!empty($name) && !empty($age)) {
$name = filter_var($name, FILTER_SANITIZE_STRING);
$age = filter_var($age, FILTER_SANITIZE_NUMBER_INT);
// Process the data (e.g., save to database, display, etc.)
echo “Name: ” . $name . “<br>”;
echo “Age: ” . $age . “<br>”;
} else {
echo “Please fill in all fields.”;
}
}
?>
[/mycode]
Explanation
- Form Submission Check:
[mycode]
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
[/mycode]
This checks if the form has been submitted using the POST method.
2.Retrieving Form Data:
[mycode]
$name = $_POST[‘name’];
$age = $_POST[‘age’];
[/mycode]
The form data is retrieved using the $_POST
superglobal array.
3.Validation and Sanitization:
[mycode]
if (!empty($name) && !empty($age)) {
$name = filter_var($name, FILTER_SANITIZE_STRING);
$age = filter_var($age, FILTER_SANITIZE_NUMBER_INT);
[/mycode]
This checks if the form fields are not empty and sanitizes the input to remove any unwanted characters.
4.Processing the Data:
[mycode]
echo “Name: ” . $name . “<br>”;
echo “Age: ” . $age . “<br>”;
[/mycode]
The sanitized data is displayed or can be processed further, like saving to a database.
Handling Form Data with GET Method
You can also use the GET method to send form data. Modify the form to use method="get"
and process the data similarly in process_form.php
.
[mycode]
<form action=”process_form.php” method=”get”>
Name: <input type=”text” name=”name”><br>
Age: <input type=”text” name=”age”><br>
<input type=”submit” value=”Submit”>
</form>
[/mycode]
In process_form.php
, use $_GET
instead of $_POST
:
[mycode]
<?php
if ($_SERVER[“REQUEST_METHOD”] == “GET”) {
$name = $_GET[‘name’];
$age = $_GET[‘age’];
if (!empty($name) && !empty($age)) {
$name = filter_var($name, FILTER_SANITIZE_STRING);
$age = filter_var($age, FILTER_SANITIZE_NUMBER_INT);
echo “Name: ” . $name . “<br>”;
echo “Age: ” . $age . “<br>”;
} else {
echo “Please fill in all fields.”;
}
}
?>
[/mycode]
Example: Form with Multiple Input Types
Create a form with various input types and handle the data in PHP.
[mycode]
<!DOCTYPE html>
<html>
<head>
<title>Extended Form Example</title>
</head>
<body>
<form action=”extended_process_form.php” method=”post”>
Name: <input type=”text” name=”name”><br>
Age: <input type=”text” name=”age”><br>
Email: <input type=”email” name=”email”><br>
Gender:
<input type=”radio” name=”gender” value=”male”> Male
<input type=”radio” name=”gender” value=”female”> Female<br>
Interests:
<input type=”checkbox” name=”interests[]” value=”coding”> Coding
<input type=”checkbox” name=”interests[]” value=”reading”> Reading
<input type=”checkbox” name=”interests[]” value=”sports”> Sports<br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
[/mycode]
In extended_process_form.php
, handle the data
[mycode]
<?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$name = $_POST[‘name’];
$age = $_POST[‘age’];
$email = $_POST[’email’];
$gender = $_POST[‘gender’];
$interests = $_POST[‘interests’];
if (!empty($name) && !empty($age) && !empty($email) && !empty($gender) && !empty($interests)) {
$name = filter_var($name, FILTER_SANITIZE_STRING);
$age = filter_var($age, FILTER_SANITIZE_NUMBER_INT);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$gender = filter_var($gender, FILTER_SANITIZE_STRING);
$interests = filter_var_array($interests, FILTER_SANITIZE_STRING);
echo “Name: ” . $name . “<br>”;
echo “Age: ” . $age . “<br>”;
echo “Email: ” . $email . “<br>”;
echo “Gender: ” . $gender . “<br>”;
echo “Interests: ” . implode(“, “, $interests) . “<br>”;
} else {
echo “Please fill in all fields.”;
}
}
?>
[/mycode]
Explanation
- Extended Form Inputs:
- Added
email
,radio
for gender, andcheckbox
for interests. interests[]
allows multiple selections, stored as an array.
- Added
- Handling Multiple Inputs:
- Retrieve and validate/sanitize all form inputs.
filter_var_array
sanitizes each value in theinterests
array.
- Displaying Processed Data:
- Use
implode
to convert theinterests
array to a comma-separated string for display.
- Use
- Next Steps Practice creating forms with various input types and processing the data in PHP. In the next section, we’ll cover working with files in PHP, including reading from and writing to files. Let me know if you have any questions or if you’re ready to move forward!