Step 8 of 10

PHP Forms

📖 Lesson

Forms + PHP

When a user submits a form, PHP processes it on the server. It can save data, send emails, or display results.

<form method="POST">
  <input name="name">
  <button>Submit</button>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $name = $_POST["name"];
  echo "Hello, " . htmlspecialchars($name);
}
?>

Saving data to files

PHP can save data directly to files on the server. This is a simple alternative to a database.

// Write to end of file
file_put_contents("messages.json",
  $newMessage, FILE_APPEND);

// Read file
$content = file_get_contents("messages.json");
htmlspecialchars() is important for security - it prevents users from injecting malicious code!

Today's project: Guestbook

In today's example we'll create a guestbook:

  • Visitors write a message and their name
  • PHP saves the message to a JSON file
  • All messages are displayed on the page
  • Data persists across restarts - it stays on the server!

This is a real web application - not just a static page!

💻 Example to try
index.php
🚀 Try on Vibmy
<?php
/**
 * Guestbook
 * Visitors can write messages that are saved to a file.
 */

$dataFile = __DIR__ . "/guestbook_data.json";

// Load existing messages
$messages = [];
if (file_exists($dataFile)) {
    $content = file_get_contents($dataFile);
    $messages = json_decode($content, true) ?: [];
}

$error = "";
$success = false;

// Process form
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $name = trim($_POST["name"] ?? "");
    $message = trim($_POST["message"] ?? "");
    $emoji = $_POST["emoji"] ?? "&#x1F44B;";

    if (strlen($name) < 2) {
        $error = "Name must be at least 2 characters.";
    } elseif (strlen($message) < 3) {
        $error = "Message must be at least 3 characters.";
    } else {
        // Add message
        array_unshift($messages, [
            "name" => htmlspecialchars($name),
            "message" => htmlspecialchars($message),
            "emoji" => $emoji,
            "date" => date("M j, Y g:i A"),
            "ip" => substr(md5($_SERVER["REMOTE_ADDR"] ?? "x"), 0, 6)
        ]);

        // Max 50 messages
        $messages = array_slice($messages, 0, 50);
        file_put_contents($dataFile, json_encode($messages, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        $success = true;
    }
}

$totalMessages = count($messages);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Guestbook</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            min-height: 100vh;
            font-family: "Segoe UI", system-ui, sans-serif;
            background: linear-gradient(135deg, #0f0c29, #1a1a3e);
            color: #e0e0e0;
            padding: 2rem;
        }
        .container { max-width: 700px; margin: 0 auto; }
        h1 {
            text-align: center;
            font-size: 2.2rem;
            background: linear-gradient(to right, #00d4aa, #7b68ee);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 0.3rem;
        }
        .subtitle { text-align: center; color: #6b7280; margin-bottom: 2rem; }
        .stats {
            text-align: center;
            margin-bottom: 2rem;
            padding: 0.8rem;
            background: rgba(0,212,170,0.08);
            border-radius: 12px;
            color: #00d4aa;
            font-weight: 600;
        }
        /* Form */
        .form-card {
            background: rgba(255,255,255,0.04);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 20px;
            padding: 2rem;
            margin-bottom: 2rem;
        }
        .form-card h2 { color: #00d4aa; margin-bottom: 1rem; font-size: 1.2rem; }
        .form-row { margin-bottom: 1rem; }
        .form-row label {
            display: block;
            color: #8892a4;
            font-size: 0.85rem;
            margin-bottom: 0.4rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 0.8rem 1rem;
            background: rgba(255,255,255,0.06);
            border: 2px solid rgba(255,255,255,0.1);
            border-radius: 12px;
            color: #e0e0e0;
            font-size: 1rem;
            font-family: inherit;
            outline: none;
            transition: border-color 0.3s;
        }
        input:focus, textarea:focus { border-color: #7b68ee; }
        textarea { min-height: 80px; resize: vertical; }
        .emoji-picker {
            display: flex;
            gap: 0.5rem;
            flex-wrap: wrap;
        }
        .emoji-option {
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.3rem;
            border-radius: 10px;
            border: 2px solid rgba(255,255,255,0.1);
            background: rgba(255,255,255,0.04);
            cursor: pointer;
            transition: all 0.3s;
        }
        .emoji-option:hover, .emoji-option.selected {
            border-color: #00d4aa;
            background: rgba(0,212,170,0.15);
            transform: scale(1.1);
        }
        .submit-btn {
            width: 100%;
            padding: 0.9rem;
            background: linear-gradient(135deg, #00d4aa, #7b68ee);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 1.05rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 0.5rem;
        }
        .submit-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(0,212,170,0.4); }
        .error-msg { background: rgba(255,107,157,0.15); color: #ff6b9d; padding: 0.8rem; border-radius: 10px; margin-bottom: 1rem; }
        .success-msg { background: rgba(0,212,170,0.15); color: #00d4aa; padding: 0.8rem; border-radius: 10px; margin-bottom: 1rem; }
        /* Messages */
        .messages h2 { color: #00d4aa; margin-bottom: 1rem; font-size: 1.2rem; }
        .message {
            background: rgba(255,255,255,0.03);
            border: 1px solid rgba(255,255,255,0.06);
            border-radius: 16px;
            padding: 1.2rem;
            margin-bottom: 1rem;
            transition: all 0.3s;
            animation: slideIn 0.3s ease;
        }
        .message:hover { border-color: rgba(0,212,170,0.2); }
        .msg-header { display: flex; align-items: center; gap: 0.8rem; margin-bottom: 0.5rem; }
        .msg-emoji { font-size: 1.5rem; }
        .msg-name { font-weight: 600; color: #a78bfa; }
        .msg-date { color: #555; font-size: 0.8rem; margin-left: auto; }
        .msg-text { color: #c0c0c0; line-height: 1.6; }
        .empty-state { text-align: center; color: #555; padding: 3rem; font-size: 1.1rem; }
        @keyframes slideIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        @media (max-width: 600px) { body { padding: 1rem; } h1 { font-size: 1.8rem; } }
    </style>
</head>
<body>
    <div class="container">
        <h1>&#x1F4D6; Guestbook</h1>
        <p class="subtitle">Leave a message for future visitors!</p>

        <?php if ($totalMessages > 0): ?>
        <div class="stats">&#x1F4AC; <?php echo $totalMessages; ?> messages total</div>
        <?php endif; ?>

        <div class="form-card">
            <h2>&#x270D;&#xFE0F; Write a message</h2>

            <?php if ($error): ?>
            <div class="error-msg"><?php echo $error; ?></div>
            <?php endif; ?>

            <?php if ($success): ?>
            <div class="success-msg">&#x2705; Message saved!</div>
            <?php endif; ?>

            <form method="POST">
                <div class="form-row">
                    <label>Name</label>
                    <input type="text" name="name" placeholder="Your name" required>
                </div>
                <div class="form-row">
                    <label>Choose emoji</label>
                    <div class="emoji-picker">
                        <div class="emoji-option selected" onclick="selectEmoji(this, '&#x1F44B;')">&#x1F44B;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F600;')">&#x1F600;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F680;')">&#x1F680;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x2764;&#xFE0F;')">&#x2764;&#xFE0F;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F31F;')">&#x1F31F;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F389;')">&#x1F389;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F4A1;')">&#x1F4A1;</div>
                        <div class="emoji-option" onclick="selectEmoji(this, '&#x1F3AE;')">&#x1F3AE;</div>
                    </div>
                    <input type="hidden" name="emoji" id="selectedEmoji" value="&#x1F44B;">
                </div>
                <div class="form-row">
                    <label>Message</label>
                    <textarea name="message" placeholder="What would you like to say?" required></textarea>
                </div>
                <button type="submit" class="submit-btn">&#x1F4E8; Post Message</button>
            </form>
        </div>

        <div class="messages">
            <h2>&#x1F4AC; Messages</h2>
            <?php if (empty($messages)): ?>
                <div class="empty-state">&#x1F4AD; No messages yet. Be the first!</div>
            <?php else: ?>
                <?php foreach ($messages as $msg): ?>
                <div class="message">
                    <div class="msg-header">
                        <span class="msg-emoji"><?php echo $msg["emoji"]; ?></span>
                        <span class="msg-name"><?php echo $msg["name"]; ?></span>
                        <span class="msg-date"><?php echo $msg["date"]; ?></span>
                    </div>
                    <div class="msg-text"><?php echo nl2br($msg["message"]); ?></div>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <script>
        function selectEmoji(el, emoji) {
            document.querySelectorAll(".emoji-option").forEach(e => e.classList.remove("selected"));
            el.classList.add("selected");
            document.getElementById("selectedEmoji").value = emoji;
        }
    </script>
</body>
</html>
← Back 8 / 10 Next step →