×

php · level 2 · unit 4 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A brings the visitor's answers to your code.

When someone visits a page, their browser sends a request. A GET request asks for a page, and any extras ride along in the address, like ?page=2. When they fill in a and press send, the browser makes a POST request carrying their answers.

PHP unpacks each request into superglobals, ready-made arrays filled in by the web server. $_GET holds the address extras, $_POST holds the form answers, and $_SERVER["REQUEST_METHOD"] says "GET" or "POST". Every value arrives as a string.

Gwecode has no real visitors, so you play the browser. Fill these arrays yourself at the top, then write the rest exactly as a real website would.

<?php
$_SERVER["REQUEST_METHOD"] = "POST";
$_POST = ["name" => "Priya", "tickets" => "2"];
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $name = $_POST["name"];
    $tickets = (int) $_POST["tickets"];
    echo "Thanks, $name! You booked $tickets tickets.\n";
} else {
    echo "Please fill in the form.\n";
}

Like $typed_name in Level 1, the pretend lines at the top stand in for a real visitor.