CodeBlanks Logo Icon CodeBlanks

Project final - Contact form

PHP course · Intermediate · Lesson 98/115

Combine all parts and finalize a working contact form.

Combining all pieces

In the final lesson you combine all parts with a processSubmission() function. It does the following in order: starts the session with session_start(), reads data from $_POST, escapes it with htmlspecialchars and fills a ContactMessage instance.

Then it runs validateMessage($m); if there are errors it returns "skip". When there are no errors it processes the file with handleUpload($_FILES['screenshot']) and stores the message in the session with $_SESSION['messages'][] = $m. Finally it returns "ok".

Storing messages in a session

The session_start() call opens the session; afterwards you can write to the $_SESSION superglobal. To store messages you write $_SESSION['messages'][] = $m; this appends each submission to the end of the list.

On later requests $_SESSION['messages'] is loaded automatically, so you can see all submitted messages in one place. The session ends when the user clears the cookie or it expires.

Exercise Write the processSubmission function

Write a processSubmission(): string function. Inside, call session_start();. Create $m = new ContactMessage();; write $m->name = htmlspecialchars($_POST['name'] ?? ''); and the same pattern for email and message. Validate with $errors = validateMessage($m);; if count($errors) > 0 return "skip". Call handleUpload($_FILES['screenshot'] ?? []);. Append to the session with $_SESSION['messages'][] = $m; and return "ok". For testing, set $_POST and $_FILES by hand; write echo processSubmission();.

Show example solution
string { session_start(); $m = new ContactMessage(); $m->name = htmlspecialchars($_POST['name'] ?? ''); $m->email = htmlspecialchars($_POST['email'] ?? ''); $m->message = htmlspecialchars($_POST['message'] ?? ''); $errors = validateMessage($m); if (count($errors) > 0) { return "skip"; } handleUpload($_FILES['screenshot'] ?? []); $_SESSION['messages'][] = $m; return "ok"; }. Set $_POST and $_FILES by hand; echo processSubmission();.
Example solution:
<?php
// Combine all pieces
class ContactMessage {
    public string $name = "";
    public string $email = "";
    public string $message = "";
}

function renderForm(): string {
    return '<form method="post" action="/contact.php">'
        . '<label>Name: <input type="text" name="name"></label>'
        . '<label>Email: <input type="email" name="email"></label>'
        . '<label>Message: <textarea name="message"></textarea></label>'
        . '<input type="submit" value="Send">'
        . '</form>';
}

function validateMessage(ContactMessage $m): array {
    $errors = [];
    if (empty($m->name)) {
        $errors[] = "Name empty";
    }
    if (empty($m->email) || filter_var($m->email, FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = "Email invalid";
    }
    return $errors;
}

function handleUpload(array $file): string {
    if ($file['error'] !== UPLOAD_ERR_OK) {
        return "skip";
    }
    if ($file['size'] > 2 * 1024 * 1024) {
        return "skip";
    }
    $dest = __DIR__ . "/uploads/" . basename($file['name']);
    move_uploaded_file($file['tmp_name'], $dest);
    return "ok";
}

function processSubmission(): string {
    session_start();
    $m = new ContactMessage();
    $m->name = htmlspecialchars($_POST['name'] ?? '');
    $m->email = htmlspecialchars($_POST['email'] ?? '');
    $m->message = htmlspecialchars($_POST['message'] ?? '');
    $errors = validateMessage($m);
    if (count($errors) > 0) {
        return "skip";
    }
    handleUpload($_FILES['screenshot'] ?? []);
    $_SESSION['messages'][] = $m;
    return "ok";
}

$_POST = ['name' => 'Alex', 'email' => 'alex@example.com', 'message' => 'Hello'];
$_FILES = ['screenshot' => ['name' => 'image.png', 'type' => 'image/png', 'tmp_name' => '/tmp/temp.tmp', 'error' => UPLOAD_ERR_OK, 'size' => 1024]];
echo processSubmission();

Test yourself

  1. Where do you store contact form messages between requests?

    • $_GET
    • $_POST
    • $_SESSION
    • $_FILES
    Show answer

    $_SESSION — Correct. $_SESSION keeps data on the server for the whole session; it survives across requests.

  2. Match the project final concepts

    session_start()
    Session start
    $_SESSION['messages']
    Message list
    validateMessage()
    Validation
    handleUpload()
    File move
  3. Complete the session_start call

    ____;
    $_SESSION['messages'][] = $m;
    Show answer
    session_start();
    $_SESSION['messages'][] = $m;

All lessons in Intermediate