Topic: PHP Basics – Part 6 of 10: Forms and User Input Handling
---
1. Introduction to Forms in PHP
• Forms are the primary way to collect data from users.
• PHP interacts with HTML forms to receive and process user input.
• Two main methods to send data:
* GET: Data is appended in the URL (visible).
* POST: Data is sent in the request body (more secure).
---
2. Creating a Basic HTML Form
•
•
---
3. Accessing Form Data in PHP
•
---
4. Validating Form Input
Validation ensures data is clean and in the expected format before processing.
---
5. Sanitizing User Input
• Prevent malicious input (e.g., HTML/JavaScript code).
• This function converts special characters to HTML entities.
---
6. Self-processing Form Example
• Using
---
7. Using the GET Method
• Data is visible in the URL:
---
8. File Upload with Forms
• Use
---
9. Summary
• PHP handles user input through forms using the
• Always validate and sanitize input to prevent security issues.
• Forms are foundational for login systems, search bars, contact pages, and file uploads.
---
Exercise
• Create a form that asks for name, age, and email, and then displays a formatted message with validation and sanitization.
---
#PHP #Forms #UserInput #POST #GET #PHPTutorial
https://t.iss.one/Ebooks2023
---
1. Introduction to Forms in PHP
• Forms are the primary way to collect data from users.
• PHP interacts with HTML forms to receive and process user input.
• Two main methods to send data:
* GET: Data is appended in the URL (visible).
* POST: Data is sent in the request body (more secure).
---
2. Creating a Basic HTML Form
<form action="process.php" method="post">
Name: <input type="text" name="username"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
•
action defines where the form data will be sent.•
method can be GET or POST.---
3. Accessing Form Data in PHP
<?php
$name = $_POST['username'];
$email = $_POST['email'];
echo "Welcome $name! Your email is $email.";
?>
•
$_GET and $_POST are superglobals that access data sent by the form.---
4. Validating Form Input
Validation ensures data is clean and in the expected format before processing.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["username"]);
if (empty($name)) {
echo "Name is required";
} else {
echo "Hello, $name";
}
}
?>
---
5. Sanitizing User Input
• Prevent malicious input (e.g., HTML/JavaScript code).
$name = htmlspecialchars($_POST["username"]);
• This function converts special characters to HTML entities.
---
6. Self-processing Form Example
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name: <input type="text" name="username"><br>
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST["username"]);
echo "Welcome, $name";
}
?>
• Using
$_SERVER["PHP_SELF"] allows the form to submit to the same file.---
7. Using the GET Method
<form action="search.php" method="get">
Search: <input type="text" name="query">
<input type="submit">
</form>
• Data is visible in the URL:
search.php?query=value---
8. File Upload with Forms
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file: <input type="file" name="myfile">
<input type="submit" value="Upload">
</form>
• Use
enctype="multipart/form-data" to upload files.<?php
if ($_FILES["myfile"]["error"] == 0) {
move_uploaded_file($_FILES["myfile"]["tmp_name"], "uploads/" . $_FILES["myfile"]["name"]);
echo "File uploaded!";
}
?>
---
9. Summary
• PHP handles user input through forms using the
GET and POST methods.• Always validate and sanitize input to prevent security issues.
• Forms are foundational for login systems, search bars, contact pages, and file uploads.
---
Exercise
• Create a form that asks for name, age, and email, and then displays a formatted message with validation and sanitization.
---
#PHP #Forms #UserInput #POST #GET #PHPTutorial
https://t.iss.one/Ebooks2023