Getting Form Field Data

Obtaining information that is entered into an HTML form is actually a simple process using PHP. All you need to know is the method used to send the form data, the name of each form field, and what you want to do with the information that you obtain.

When the form method used is "get", the information entered into the form is sent via the URL when the form is submitted. It is visible to everyone, and limited to 100 characters.

Using the $_GET superglobal along with the name of the form field, we can obtain the value of each field.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
  Type In Something: <input name="random-info" type="text" size="25">
  <input type="submit" value="Submit">
</form> <br>

<?php
  echo "You Typed: " . $_GET['random-info'];
?>

Type In Something:

If you tried the demo above you will see that the HTML form, when submitted, uses $_SERVER['PHP_SELF'] to send you right back to the page that the form was submitted from. If you had typed anything into the form, it is echoed directly onto the page, and can also be seen at the end of the URL if you look in the address bar.

Obviously, using the GET method is limiting in both size and security, so use it wisely. It is useful if you want to allow your users to bookmark a page to come back to later, because they will be returned to the page with the same information already submitted.