PHP Forms

PHP Forms

In this tutorial we are going to learn about $_GET, $_POST, and $_REQUEST. Nowadays, PHP is one of the most popular means of processing HTML forms. To read the data submitted by a form, using PHP, use one of three special arrays: $_GET, $_POST or $_REQUEST. The first two contain the data submitted via their respective protocols. The third, $_REQUEST contains the data from both. The index of the array is the name property of the HTML input element. Therefore, the value used in input name=" " in the HTML form must be the same as the array value in the $_GET[' '] or $_POST[' '] arrays in the PHP code.

For the $_GET function

$_GET function is used to collect values from a form sent with the method set to "get". When using this method in HTML forms, all variable names and values are displayed in the URL. Therefore, this method should not be used when sending passwords or other sensitive information. Example This example shows the basic use of the $_GET function:
  1. <form action="get.php" method="get">
  2. First Name:
  3. <input type="text" name="f_name" />
  4. Last Name:
  5. <input type="text" name="l_name" />
  6. <input type="submit" />
  7. </form>
The "get.php" file can now use the $_GET function to collect data from the form:
  1. First Name:
  2. <?php echo $_GET["f_name"]; ?>
  3. <br />
  4. Last Name:
  5. <?php echo $_GET["l_name"]; ?>
Note: The get method is not suitable for large variable values; the value cannot exceed 100 characters.

For the $_POST function

$_POST function is used to collect values from a form sent with the method set to "post". When using this method in HTML forms, the information sent from a form is not visible in the URL and there is no limit on the information that can be sent. Example This example shows the basic use of the $_POST function:
  1. <form action="post.php" method="post">
  2. First Name:
  3. <input type="text" name="f_name" />
  4. Last Name:
  5. <input type="text" name="l_name" />
  6. <input type="submit" />
  7. </form>
The "post.php" file can now use the $_POST function to collect data from the form:
  1. First Name:
  2. <?php echo $_POST["f_name"]; ?>
  3. <br />
  4. Last Name:
  5. <?php echo $_POST["l_name"]; ?>

For the $_REQUEST function

$_REQUEST function contains the contents of $_GET, $_POST, and $_COOKIE. It can be used to collect form data sent with either the GET or POST methods. In the PHP file, the $_REQUEST function can be used to collect data from the form:
  1. First Name:
  2. <?php echo $_REQUEST["f_name"];?>
  3. <br />
  4. Last Name:
  5. <?php echo $_REQUEST["l_name"];?>

Add new comment