0% found this document useful (0 votes)
4 views13 pages

Unit V (Handling Forms)

The document provides an overview of handling forms in PHP, including the structure of HTML forms, methods for retrieving form data using $_GET, $_POST, and $_REQUEST, and the steps for processing user input. It also discusses the importance of validating and sanitizing input to enhance security, as well as setting response headers for actions like redirection and file downloads. Overall, it serves as a guide for creating and managing forms in web applications using PHP.

Uploaded by

ukesh bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Unit V (Handling Forms)

The document provides an overview of handling forms in PHP, including the structure of HTML forms, methods for retrieving form data using $_GET, $_POST, and $_REQUEST, and the steps for processing user input. It also discusses the importance of validating and sanitizing input to enhance security, as well as setting response headers for actions like redirection and file downloads. Overall, it serves as a guide for creating and managing forms in web applications using PHP.

Uploaded by

ukesh bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Handling Forms(Unit V)

Prepared By: Ukesh Bhattarai


Forms
 A form is an interactive user interface that collects input from users and sends it to a
server for processing.
 Forms use the <form> element in HTML and can be processed using PHP.
 Basic Form Structure:
<form action="process.php" method="post">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Submit">
</form>
Explanation
<form action="process.php" method="post"> → Sends data to process.php
<input type="text" name="name"> → Text field for name
<input type="email" name="email"> → Email field for email
<input type="submit"> → Button to submit the form
Handling Forms in PHP
Form handling refers to the process of collecting, processing, and validating user
input in web applications.
PHP handles form data using $_GET, $_POST, and $_REQUEST superglobal arrays.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
echo "Name: " . $name . "<br>";
echo "Email: " . $email;
}
?>
$_POST["name"] → Retrieves the name field value.
$_POST["email"] → Retrieves the email field value.
$_SERVER["REQUEST_METHOD"] == "POST" → Ensures data is submitted via POST.
Using different Input Types
<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="password" name="password"><br>
Gender:
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female <br>
Hobbies:
<input type="checkbox" name="hobby[]" value="Reading"> Reading
<input type="checkbox" name="hobby[]" value="Sports"> Sports <br>
Country:
<select name="country">
<option value="Nepal">Nepal</option>
<option value="India">India</option>
</select><br>
<input type="submit" value="Submit">
</form>
<input type="password"> → Hides characters for password fields.
<input type="radio"> → Allows one selection (e.g., gender).
<input type="checkbox"> → Allows multiple selections.
Retrieving form data
 1. Retrieving Data Using $_GET
 Used when the form method is GET.
 Best for non-sensitive data.
 Example:
<form action="process.php" method="get">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<?php
echo "Hello, " . $_GET['name'];
?>
The URL will appear as:
example.com/process.php?name=John
 2. Retrieving Data Using $_POST
 Used when the form method is POST.
 Suitable for forms with passwords, login, and large data.
<form action="process.php" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<?php
echo "Hello, " . $_POST['name'];
?>
The URL remains clean without showing the data.
 3. Retrieving Data Using $_REQUEST
 Can retrieve data from both $_GET and $_POST.
 Not recommended for security-sensitive applications.
<?php
if(isset($_REQUEST['name'])) {
echo "Hello, " . $_REQUEST['name'];
}
?>
Works with both GET and POST forms.
Processing forms in PHP

 Processing forms involves handling user input after submission.


 This includes retrieving data, validating it, and displaying or storing it in
a database.
 Steps to process forms in PHP:
 Create an HTML form (with GET or POST method).
 Handle form submission in PHP using $_GET or $_POST.
 Validate user input (e.g., checking if fields are empty).
 Sanitize input to prevent security issues.
 Process the data (e.g., store in a database, send an email).
 Display the processed output.
 Processing a simple form:
 Create an HTML form:
<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
 Process form data:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieving form data
$name = $_POST['name'];
$email = $_POST['email'];

// Validating input
if (empty($name) || empty($email)) {
echo "All fields are required!";
} else {
// Sanitizing input
$name = htmlspecialchars($name);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

echo "Hello, " . $name . "! Your email is " . $email;


}
}
?>
Setting response headers

 Response headers are used to send additional information to the browser


before the actual content is sent.
 In PHP, we use the header() function to set response headers.
 The header() function must be called before any output (HTML, echo, etc.).
 1. Why Use Response Headers?
 Redirecting users to another page
 Forcing file downloads

Syntax:
header("Header-Type: value");
 Redirecting to another page:
header("Location: https://www.example.com");
exit(); // Stops script execution after redirection

 Forcing file download:


header("Content-Disposition: attachment; filename=file.txt");
header("Content-Type: text/plain");
echo "This is a downloaded file.";
 Checking Headers Sent
If you accidentally send headers after output, PHP throws an error. Use:
if (!headers_sent()) {
header("Location: welcome.php");
} else {
echo "Headers already sent!";
}

You might also like