PHP Introduction | Form Submission Basics | Sending a Form

Writing a server-side program can be described as writing something that exchanges information between a client and a server. Here, the client is the program that accesses the server. In other words, it means the Web browser. Server-side programs are needed when you build programs that operate while exchanging various information between the browser and the server. If something runs only inside the browser, JavaScript is enough. A server-side program is not required.

When information is exchanged between the client and the server, the most commonly used mechanism is the “form.” This is the familiar HTML mechanism that displays input fields and buttons and sends their values. You prepare a form, send it to the server, process the sent values in a server program, and return the result to the client. This is how Web applications work.

Now let us use PHP to process data sent from a form. We will write a simple example like the following.

<?php
    $str = $_POST['text1'];
    if ($str != null){
        $result = "You wrote '{$str}'.";
    } else {
        $result = "Nothing was written.";
    }
?>
<!DOCTYPE html>
<html lang="ko">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
        <title>sample page</title>
    </head>
    <body>
        <h1>Hello PHP!</h1>
        <div><?php echo $result; ?></div>
        <form method="post" action="./index.php">
            <input type="text" name="text1">
            <input type="submit">
        </form>
    </body>
</html>

Access this from a Web browser.

Here, the form is prepared in the following way.

  • <form> tag

    • method is "post" and action is "./index.php". The method indicates the transmission method. It specifies either GET or POST from the HTTP protocol. GET is normally used to receive page content, while POST is used to write data. Either can send data, but here we specify POST.
    • action specifies the destination for submitting the form. Here, it submits to this index.php file itself. In other words, when the form is submitted, the server reads this same file again and processes the form sent to it.
  • <input type="text"> tag

    • This tag is for text input. Here, name="text1" is specified. This is important because, when the form is sent to the server, the submitted data is identified by this name.
  • <input type="submit"> tag

    • This is the submit button. It does not especially need settings such as name or id, because it has no effect on the server side other than submitting the form.

Submitted Forms and $_GET, $_POST

Where and how is the submitted form content received? The receiving part is inside the <?php ?> tag at the beginning of the file. There, the following processing is performed.

$str = $_POST['text1'];

$_POST is an array that stores the contents of the submitted form. Do you remember arrays? They are special variables that can manage many values at once. Arrays are managed by indexes, and associative arrays are managed by values called keys. In short, $_POST is an associative array.

This time the form was sent by POST, so the contents were gathered in $_POST. If it had been sent by GET, they would be gathered in the $_GET array. These two associative arrays are used to manage form contents.

In this array, the form contents are organized by the names specified in the name attributes. Since there is a tag with the attribute name="text1", the value is stored in a form such as $_POST['text1'].

One point to remember is that arrays provided by the PHP system, such as $_GET and $_POST, can only be used to retrieve values. For example, even if you try to rewrite this value and use it to set the value displayed in the form, that will not work. Think of them simply as things that deliver submitted data.

Writing Text with Variable Interpolation

Here, another technique is also being used. Look at the part at the beginning of the script where text is assigned to $result.

$result = "You wrote '{$str}'.";

It is written like this. {$str} inserts the value of the variable $str into the text. In other words, text is created with the content of $str added at that point.

Text enclosed in double quotation marks (") can contain variables like this. This means you can create text using various variables.

On the other hand, if you want to output the literal text $str, use single quotation marks (') to create the text. Text in single quotation marks does not perform interpolation. You get the text exactly as written.

echo 'Variables do not $str';

For example, if you write the above, the screen displays Variables do not $str exactly as it is.

Make sure you remember the difference between these two ways of writing text.