Mada za sehemu hiiWorking with formsMada 2
- Creating forms and handle form data
- Performing validation and sanitation of user input
Creating and Handling Form Data
HTML forms are the primary way web applications collect information from users. Whether users are submitting a contact message, registering for a service, or answering a survey, forms gather the data that web applications process. In PHP, we use special variables called superglobals to retrieve and handle this data on the server side.
An HTML form is created using the <form> element, which acts as a container for various input controls. Every form needs two essential attributes:
- method — specifies how the form data is sent to the server
- action — specifies the URL of the script that will process the data
Common Input Types
| Input Type | Purpose |
|---|---|
<input type="text"> | Single-line text entry |
<input type="email"> | Email address |
<input type="password"> | Hidden text for passwords |
<input type="number"> | Numeric values |
<textarea> | Multi-line text |
<select> | Dropdown selection |
<input type="submit"> | Submit button |
Each input field should have a name attribute, which becomes the key used to retrieve the value in PHP.
GET Method
- Appends form data to the URL as a query string
- Data is visible in the browser address bar
- Useful for non-sensitive data and searches
- Example URL:
process.php?name=John&age=20
POST Method
- Sends form data in the HTTP request body
- Data is not visible in the URL
- Required for sensitive data (passwords, personal information)
- Preferred for most form submissions
PHP provides two superglobal arrays to access form data:
$_GET— contains data sent using the GET method$_POST— contains data sent using the POST method
Each input field's name attribute becomes the array key.
Basic Syntax
// For a form field with name="username"
$username = $_POST['username'];
// For a form field with name="email"
$email = $_POST['email'];
Step 1: Create the HTML Form (contact.html)
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<h2>Contact Us</h2>
<form method="POST" action="process.php">
<label>Name:</label><br>
<input type="text" name="name" required><br>
<label>Email:</label><br>
<input type="email" name="email" required><br>
<label>Message:</label><br>
<textarea name="message" rows="5" required></textarea><br><br>
<input type="submit" value="Send Message">
</form>
</body>
</html>
Step 2: Process the Form Data (process.php)
<?php
// Retrieve data from the form
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Display the received data
echo "<h2>Thank you, $name!</h2>";
echo "<p>Your email: $email</p>";
echo "<p>Your message: $message</p>";
?>
Key Points in This Example
- The form uses
method="POST"for secure data transmission - The
action="process.php"tells the browser where to send the data - Each input has a unique
nameattribute - The
requiredattribute forces users to fill in all fields - PHP retrieves values using
$_POST['fieldname']
When processing form data, always:
- Validate input — check that required fields are not empty
- Sanitize data — clean user input to prevent attacks
<?php
$name = trim($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (empty($name)) {
echo "Name is required";
}
?>
In Tanzania, small businesses like dukas (shops) and restaurants can use web forms to collect orders and reservations. For example, a hotel in Arusha might have a booking form where customers submit their names, phone numbers, and dates. The PHP script processes this data and stores it, allowing the hotel staff to view all reservations in one place instead of relying on paper records or phone calls. This makes managing bookings faster and more organized, especially during peak tourist season when the hotel receives many inquiries daily.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Create forms with appropriate elements (text inputs, dropdowns, checkboxes, etc.).
- Retrieve and display form data using PHP.
- Use proper form attributes for data submission.
Tathmini ya matokeo
Functional forms that can accept, submit, and display user input (e.g., a contact form submitting data to the server).
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to
- HTTP requests work and how data is passed using GET and POST.
Principles: The student should explain the principles of:
- structuring forms to ensure usability and accessibility.
Theories: The student should explain:
- form behaviours and methods
- client-server model in the context of forms, focusing on how data flows between the browser and server.
Circumstantial knowledge:
Detailed knowledge about:
- Data types
Swali
What two attributes are essential for every HTML <form> element?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado