How to handle the HTML form submission in PHP using GET method

Overview

Forms are used to collect data from the users. In this shot, we'll learn how to work with HTML forms using the HTTP method, GET.

We'll learn:

  • The HTML form reminder

  • The GET method

  • Practice

  • Final notes

The HTML form reminder

This is how a form looks like in HTML:

<html>
<body>
<form action="handle_form.php" method="GET">
<input type="text" name="name" placeholder="Username here..."> <br/>
<input type="email" name="email" placeholder="Email here..."> <br/>
<input type="submit">
</form>
</body>
</html>

As you can see in the code above, the form tag has two properties, action and method. Let's learn more about them.

  • action: This specifies the file or page that the form is submitted to. In our example, the form data will be submitted to the handle_form.php file.

  • method: This describes the transport means used to send data, mostly GET or POST.

In the form above, once the user submits the data, it will be sent to handle_form.php using the GET method.

Note:

  • In case we want the form data be handled in the same file, we can leave the action attribute empty: <form action="" >.

  • If no method is specified, GET is used by default.

The GET method

When GET is used as a method, data is submitted as URL parameters to the handler server or page. In other words, the form is converted to a link containing all the values entered by the user.

Let's suppose that the user sent this data:

  • name: Sarah

  • email: salimas@gmail.com

On submission, the user will be redirected to the following URL:

In the handle_form.php file, the submitted data is contained in a special variable called $_GET. This last variable is an associative array where keys are the names of fields sent. So, for our example above, we have the following:

Name

Value

$_GET['name']

Sarah

$_GET['email']

salimas@gmail.com

Note: The array key name is the name property in the HTML form field.

Let's now implement the code for handle_form.php:

<?php
// handle_form.php
echo '<h1>Welcome at Educative</h1>';
echo '<p>Username: ' . $_GET['name'] . '</p>';
echo '<p>Email: ' . $_GET['email'] . '</p>';

Practice

Let's practice the GET method by putting all the previous code together.

handle_form.php
index.php
<?php
// handle_form.php
$user_name = $_GET['name'];
$email = $_GET['email'];
// Basic control
if (!isset($user_name) || !isset($email))
{
echo 'Name or email field is empty.';
// Stop PHP execution
return;
}
echo '<h1>Welcome at Educative</h1>';
echo '<p>Username: ' . $user_name . '</p>';
echo '<p>Email: ' . $email . '</p>';

In handle_form.php, we introduce a basic form validation with isset(). Learn more about PHP validation here.

Final notes

All submitted data is displayed in the URL. This may not be useful for us, especially if we have to collect sensitive data like passwords.

Additionally, GET has limits on the amount of data to send, which is 2000 characters. Therefore, if our form has a large number of fields, GET is not the appropriate method to use. In the real world, we use it for small forms like search forms with one or two fields.



Free Resources