📅 Week 9 - Day 3: Handling GET & POST Methods

Friday | Duration: 3 hours

🎯 Learning Objectives

📨 HTTP Methods: GET vs POST

Feature GET POST
Visibility Data visible in URL Data hidden in request body
Security Less secure More secure
Data Size Limited (~2000 characters) Unlimited
Bookmarking Can be bookmarked Cannot be bookmarked
Use Case Search queries, filters Login forms, file uploads

🔤 GET Method

HTML Form with GET

<!-- search_form.html --> <!DOCTYPE html> <html> <head> <title>Search Form</title> </head> <body> <h2>Search Products</h2> <form action="search.php" method="GET"> <label>Product Name:</label> <input type="text" name="product" required> <br><br> <label>Category:</label> <select name="category"> <option value="electronics">Electronics</option> <option value="clothing">Clothing</option> <option value="books">Books</option> </select> <br><br> <button type="submit">Search</button> </form> </body> </html>

Processing GET Data in PHP

<?php // search.php if (isset($_GET['product']) && isset($_GET['category'])) { $product = $_GET['product']; $category = $_GET['category']; echo "<h2>Search Results</h2>"; echo "<p>Searching for: <strong>" . $product . "</strong></p>"; echo "<p>Category: <strong>" . $category . "</strong></p>"; // URL will look like: search.php?product=laptop&category=electronics } ?>

📮 POST Method

HTML Form with POST

<!-- login_form.html --> <!DOCTYPE html> <html> <head> <title>Login Form</title> <style> body { font-family: Arial, sans-serif; max-width: 400px; margin: 100px auto; padding: 20px; } input { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ddd; border-radius: 5px; } button { width: 100%; padding: 12px; background: #ff6b6b; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background: #ff5252; } </style> </head> <body> <h2>Login to Your Account</h2> <form action="login.php" method="POST"> <label>Username:</label> <input type="text" name="username" required> <label>Password:</label> <input type="password" name="password" required> <button type="submit">Login</button> </form> </body> </html>

Processing POST Data in PHP

<?php // login.php if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['username']; $password = $_POST['password']; // Simple validation if ($username == "admin" && $password == "password123") { echo "<h2>Welcome, " . $username . "!</h2>"; echo "<p>Login successful.</p>"; } else { echo "<h2>Login Failed</h2>"; echo "<p>Invalid username or password.</p>"; } } ?>

🛡️ Input Validation & Security

Basic Validation

<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $email = $_POST['email']; $age = $_POST['age']; // Check if email is empty if (empty($email)) { echo "Email is required!"; } // Validate email format elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Invalid email format!"; } // Check if age is numeric elseif (!is_numeric($age)) { echo "Age must be a number!"; } // Check age range elseif ($age < 18 || $age > 100) { echo "Age must be between 18 and 100!"; } else { echo "Form submitted successfully!"; } } ?>

Sanitizing Input

<?php // Remove HTML tags and special characters $name = htmlspecialchars($_POST['name']); // Remove whitespace $username = trim($_POST['username']); // Convert to lowercase $email = strtolower($_POST['email']); // Strip tags $comment = strip_tags($_POST['comment']); ?>

💻 Complete Example: Registration Form

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Registration Form</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 50px; } .form-container { max-width: 500px; margin: 0 auto; background: white; padding: 40px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); } h2 { color: #667eea; margin-bottom: 30px; text-align: center; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 5px; font-weight: bold; color: #333; } input, select, textarea { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px; font-size: 14px; } input:focus, select:focus, textarea:focus { outline: none; border-color: #667eea; } button { width: 100%; padding: 15px; background: #667eea; color: white; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; font-weight: bold; } button:hover { background: #5568d3; } .error { color: red; font-size: 14px; margin-top: 5px; } .success { background: #d4edda; color: #155724; padding: 20px; border-radius: 5px; margin-bottom: 20px; border: 1px solid #c3e6cb; } </style> </head> <body> <div class="form-container"> <h2>Student Registration</h2> <?php $errors = []; $success = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get form data $fullname = trim($_POST['fullname']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $age = $_POST['age']; $gender = $_POST['gender']; $course = $_POST['course']; // Validation if (empty($fullname)) { $errors[] = "Full name is required"; } elseif (strlen($fullname) < 3) { $errors[] = "Name must be at least 3 characters"; } if (empty($email)) { $errors[] = "Email is required"; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = "Invalid email format"; } if (empty($phone)) { $errors[] = "Phone number is required"; } elseif (!is_numeric($phone) || strlen($phone) != 11) { $errors[] = "Phone must be 11 digits"; } if (empty($age) || $age < 16 || $age > 60) { $errors[] = "Age must be between 16 and 60"; } if (empty($gender)) { $errors[] = "Please select gender"; } if (empty($course)) { $errors[] = "Please select a course"; } // If no errors, show success if (empty($errors)) { $success = "Registration successful!<br>"; $success .= "Name: " . htmlspecialchars($fullname) . "<br>"; $success .= "Email: " . htmlspecialchars($email) . "<br>"; $success .= "Phone: " . htmlspecialchars($phone) . "<br>"; $success .= "Age: " . $age . "<br>"; $success .= "Gender: " . $gender . "<br>"; $success .= "Course: " . $course; } } // Display errors if (!empty($errors)) { echo "<div style='background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; margin-bottom: 20px;'>"; foreach ($errors as $error) { echo "<p>• " . $error . "</p>"; } echo "</div>"; } // Display success if (!empty($success)) { echo "<div class='success'>" . $success . "</div>"; } ?> <form method="POST" action=""> <div class="form-group"> <label>Full Name:</label> <input type="text" name="fullname" placeholder="Enter full name"> </div> <div class="form-group"> <label>Email:</label> <input type="email" name="email" placeholder="Enter email"> </div> <div class="form-group"> <label>Phone Number:</label> <input type="text" name="phone" placeholder="08012345678"> </div> <div class="form-group"> <label>Age:</label> <input type="number" name="age" min="16" max="60"> </div> <div class="form-group"> <label>Gender:</label> <select name="gender"> <option value="">Select Gender</option> <option value="Male">Male</option> <option value="Female">Female</option> </select> </div> <div class="form-group"> <label>Course:</label> <select name="course"> <option value="">Select Course</option> <option value="Web Development">Web Development</option> <option value="Mobile Development">Mobile Development</option> <option value="Data Science">Data Science</option> </select> </div> <button type="submit">Register</button> </form> </div> </body> </html>

✏️ Practice Exercises

  1. Create a search form using GET method with 2 input fields
  2. Build a contact form using POST with name, email, and message
  3. Create a calculator form that adds two numbers (POST method)
  4. Build a login form with validation for username and password
  5. Create a feedback form with rating (1-5) and comments

🔒 Security Best Practices

✅ Today's Deliverable

← Previous Course Home Next: Week 10 →