PHP Form Handling

The $GET and $POST superglobals in PHP are used to gather form-data. The request for the form can be received or uploaded. We need to use $GET to retrieve data from get request, $POST for post request.


1) $_GET

The GET method sends encoded user information attached to the request for a page.
On the URL client the data passed through get request is available so it is not protected.

HTML FILE: 
<form method=”get” action = “ ”> 
<input type="text" name="email">
</form>
PHP FILE:
$get_email=$_GET["email"];

In the URL the information sent through the $GET method is available. For example, if you enter in the form above an email from [email protected] and send the address, this is how URL will look at the localhost:

http://localhost/test.php?email=dpkrwt%00gmail.com

2) $_POST

The system POST transmits information through HTTP headers.

ASCII as well as binary data can be submitted using the POST process.

Post request is commonly used to send form which has large amounts of data such as uploading images, uploading files, login form, registration form etc.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['name'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="name">
  <input type="submit">
</form>

Diffrence between $_GET and $_POST Methods