Courses/PHP/Forms & User Input

    Lesson 9 โ€ข Intermediate

    Forms & User Input ๐Ÿ“

    Handle HTML form submissions securely โ€” sanitize, validate, and process user data with PHP's built-in filter functions.

    What You'll Learn in This Lesson

    • โ€ข GET vs POST: when to use each method
    • โ€ข Sanitizing input: htmlspecialchars, trim, strip_tags
    • โ€ข Validating with filter_var and custom rules
    • โ€ข Sticky forms that preserve user input on errors
    • โ€ข CSRF protection with tokens

    Try It: Form Processing

    Sanitize and validate a contact form submission step by step

    Try it Yourself ยป
    JavaScript
    // PHP Forms & User Input (simulated in JavaScript)
    console.log("=== GET vs POST Methods ===");
    console.log();
    
    console.log("GET  โ€” data in URL: example.com/search?q=php&page=2");
    console.log("POST โ€” data in request body (hidden from URL)");
    console.log();
    
    console.log("Feature      | GET                  | POST");
    console.log("โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€");
    console.log("Visibility   | In URL               | Hidden");
    console.log("Size limit   | ~2048 chars        
    ...

    Try It: Validation & Security

    Use PHP filter functions, sticky forms, and CSRF protection

    Try it Yourself ยป
    JavaScript
    // Advanced Form Validation Patterns
    console.log("=== PHP Filter Functions ===");
    console.log();
    console.log("PHP has built-in filter functions for validation:");
    console.log();
    
    // Simulate filter_var validations
    let testCases = [
        { input: "alice@example.com", filter: "FILTER_VALIDATE_EMAIL", valid: true },
        { input: "not-an-email", filter: "FILTER_VALIDATE_EMAIL", valid: false },
        { input: "https://example.com", filter: "FILTER_VALIDATE_URL", valid: true },
        { input: "example.com"
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Trusting client-side validation โ€” JavaScript validation is for UX only. Always validate on the server โ€” users can disable JavaScript!
    โš ๏ธ
    Echoing raw $_POST/$_GET โ€” always use htmlspecialchars() to prevent XSS attacks.
    โš ๏ธ
    Not checking REQUEST_METHOD โ€” form handlers should verify $_SERVER['REQUEST_METHOD'] === 'POST' before processing.
    ๐Ÿ’ก
    Pro Tip: Always call exit; after header('Location: ...') to prevent the rest of the page from executing.

    ๐Ÿ“‹ Quick Reference โ€” Forms

    FunctionPurpose
    $_GET / $_POSTAccess form data
    htmlspecialchars()Prevent XSS (escape HTML)
    filter_var()Validate/sanitize specific types
    trim() / strip_tags()Clean whitespace/HTML
    $_SERVER['REQUEST_METHOD']Check GET or POST

    ๐ŸŽ‰ Lesson Complete!

    You can now handle form data securely! Next, learn how to persist data between pages with sessions and cookies.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service