As PHP version 5.5 is no longer supported by the community, we strongly recommend new apps use the PHP 7 runtime.

Handling User Input in Forms

This page shows you how to allow users to post their own greetings to the guestbook in a web form.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Creating a Guestbook.

Posting message content

In the following code sample, when the form is submitted, the application receives a request using the HTTP POST method and PHP makes POSTed form variables available using the $_POST superglobal.

The message is stored in content. Before displaying the message, the application checks to see if is present in the $_POST superglobal:

if (array_key_exists('content', $_POST)) {
    echo "You wrote:<pre>\n";
    echo htmlspecialchars($_POST['content']);
    echo "\n</pre>";

If the message is present, then special HTML characters in the message are replaced with their corresponding HTML entities using the htmlspecialchars function. For example, "<" is replaced by "&lt;".