Sending data using GET and POST in PHP
When dealing with the forms, information can be submitted and transferred to same or another page. To send submitted data through form, we can use GET and POST method to do that in PHP. Form data can be submitted using these two methods. Both are used for same purpose but stands apart under some specifications. As in GET method key values are passed in the Url while in POST, the information transfers in a hidden manner.
Before the browser sends the information, it encodes it using a scheme called URL encoding. In this scheme, key / value pairs are joined with equal signs and different pairs are separated by the ampersand. Spaces are removed and replaced with the + character and any other non alphanumeric characters are replaced with a hexadecimal values. After the information is encoded it is sent to the server.
GET method
In GET method the data is sent as URL parameters that are usually strings of name and value pairs separated by &. The GET method produces a long string that appears in your server logs, in the browser's Location: box. The GET method can send a maximum of 1024 characters. We can't use GET to send binary data, like images or word documents, to the server. The data sent by GET method can be accessed using the QUERY_STRING environment variable. To access the data sent using the GET method, PHP provides $_GET associative array.
<?php if( $_GET["name"] || $_GET["age"] ) { echo "Welcome ". $_GET['name']. "<br />"; echo "You are ". $_GET['age']. " years old."; exit(); } ?> <html> <body> <form action = "<?php $_PHP_SELF ?>" method = "GET"> Name: <input type = "text" name = "name" /> Age: <input type = "text" name = "age" /> <input type = "submit" /> </form> </body> </html>
POST method
The POST method sends information through the HTTP headers. The information is encoded like the GET method and is put into a header called QUERY_STRING. Unlike the GET method, the POST method does not have a limit on how much data we can send. We can send both ASCII and binary data. The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP we can make sure that our information is secure. To access the data sent using the POST method, PHP provides $_POST associative array.
<?php if( $_POST["name"] || $_POST["age"] ) { echo "Welcome ". $_POST['name']. "<br />"; echo "You are ". $_POST['age']. " years old."; exit(); } ?> <html> <body> <form action = "<?php $_PHP_SELF ?>" method = "POST"> Name: <input type = "text" name = "name" /> Age: <input type = "text" name = "age" /> <input type = "submit" /> </form> </body> </html>