🏠 Home / Hub

🐘 PHP Lesson 07 — Forms & GET/POST

← Back to PHP Menu

1. GET vs POST

$_GET$_POST
Data locationURL query stringRequest body (hidden)
URL examplepage.php?name=MinNot visible in URL
Size limit~2048 chars (URL)Large (default 8MB)
Bookmark-able✅ Yes❌ No
SecurityLess secure (visible)More secure
Use forSearch, filters, paginationLogin, registration, sensitive

2. Basic Form Handling

<!-- HTML file: contact.php -->
<!DOCTYPE html>
<html>
<body>
<form action="contact.php" method="POST">
    <input type="text" name="name" placeholder="Your name">
    <input type="email" name="email" placeholder="Email">
    <button type="submit" name="submit">Send</button>
</form>

<?php
// Same file processes itself (self-referencing)
if (isset($_POST['submit'])) {
    $name  = htmlspecialchars($_POST['name'] ?? '');
    $email = htmlspecialchars($_POST['email'] ?? '');

    echo "Hello, $name! Email: $email";
}
?>
</body>
</html>
💡 action="contact.php" — same file process လုပ်တာ "self-referencing form" ဆိုတယ်

3. Complete Form with Validation

<?php
$errors = [];
$success = false;

// Sanitize functions
function clean($data) {
    return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}

if (isset($_POST['submit'])) {
    $name  = clean($_POST['name'] ?? '');
    $email = clean($_POST['email'] ?? '');
    $age   = (int)($_POST['age'] ?? 0);

    // Validate
    if (empty($name))
        $errors[] = "Name required";
    elseif (strlen($name) < 2)
        $errors[] = "Name min 2 chars";

    if (empty($email))
        $errors[] = "Email required";
    elseif (!filter_var($email, FILTER_VALIDATE_EMAIL))
        $errors[] = "Invalid email format";

    if ($age < 1 || $age > 120)
        $errors[] = "Valid age required";

    if (empty($errors)) {
        $success = true;
        // Save to database after validation passes
    }
}
?>

<!-- HTML template -->
<?php if ($success): ?>
    <p style="color:green">✅ Registration successful!</p>
<?php else: ?>
    <?php foreach ($errors as $error): ?>
        <p style="color:red">❌ <?= e($error) ?></p>
    <?php endforeach; ?>
    <form method="POST">
        <input name="name" value="<?= e($name ?? '') ?>">
        <!-- Keep form values after error! -->
    </form>
<?php endif; ?>

4. PHP Superglobals

VariableContents
$_GETURL query parameters
$_POSTPOST form data
$_REQUESTGET + POST + COOKIE combined
$_FILESUploaded files
$_SESSIONSession data
$_COOKIECookie data
$_SERVERServer info (IP, method, headers)
$_ENVEnvironment variables
$GLOBALSAll global variables
<?php
// $_SERVER examples
echo $_SERVER['REQUEST_METHOD'];  // "GET" or "POST"
echo $_SERVER['REMOTE_ADDR'];    // User's IP address
echo $_SERVER['PHP_SELF'];        // Current script path
echo $_SERVER['HTTP_USER_AGENT']; // Browser info

// Check request method
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Handle POST
}
?>

5. File Upload

<!-- HTML -->
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="photo" accept="image/*">
    <button type="submit">Upload</button>
</form>

<?php
if (isset($_FILES['photo'])) {
    $file = $_FILES['photo'];

    // $_FILES structure:
    // ['name']     → original filename
    // ['type']     → MIME type e.g. "image/jpeg"
    // ['size']     → size in bytes
    // ['tmp_name'] → temp path on server
    // ['error']    → 0 = no error

    if ($file['error'] === 0) {
        $allowed = ['image/jpeg', 'image/png', 'image/webp'];
        if (in_array($file['type'], $allowed) && $file['size'] < 2000000) {
            $dest = "uploads/" . basename($file['name']);
            move_uploaded_file($file['tmp_name'], $dest);
            echo "✅ Uploaded!";
        }
    }
}
?>

🎉 PHP Core 7 Lessons Complete!

Next: OOP Classes, Inheritance, Interfaces, Traits, Namespaces, Exceptions ဆက်သင်မယ်

PHP 08 · OOP Classes → 🗄️ SQL + MySQL → 🚀 Back to Hub

← PHP 06  |  PHP 08 → OOP Classes

📌 Study Checklist