PHP gives there is 3 types of request methods to by which client can send information to server. In this article we will learn difference in get vs post and also in request. So, Methods are as follow
1. $_POST Request Method
$_POST is used to fetch the post request data from the client end.
Example of client and server request in php
<form action="/action_page.php" method="post"> <label for="fname">First name:</label> <input type="text" id="fname" name="fname"><br><br> <input type="submit" value="Submit"> </form>
<?php $first_name = $_POST['fname'];
2. $_GET Request Method
$_GET is used to fetch the query params from URL and form.
<form action="/action_page.php" method="get"> // here method is get <label for="fname">First name:</label> <input type="text" id="fname" name="fname"><br><br> <input type="submit" value="Submit"> </form>
we can also use it in URL
<?php $first_name = $_GET['fname']; //form index.php //url $post= $_GET["post"]; $action=$GTE["action"];
3. $_REQUEST Method
$_REQUEST is combination of both $_GET and $_POST. so we can fetch both type of request using $_REQUEST.
<?php $first_name = $_REQUEST['fname']; //form index.php //url $post= $_REQUEST["post"]; $action=$_REQUEST["action"];
Difference between GET vs POST
Here is table for get vs post
|BACK button/Reload||No Loss||Post Data will resubmit|
|Bookmarked||We can bookmark easily||Cannot|
|Encoding type||application/x-www-form-urlencoded||application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data|
|History||Parameters stay in url||Lost|
|Restrictions on data length||Yes, maximum URL length is 2048 characters||No restrictions|
|Restrictions on data type||Only ASCII characters allowed||No restrictions|
|Visibility||Yes, visible in url||No|
Difference between $_GET and $_REQUEST
As i mentioned above $_REQUEST is combination of both post and get. Hence, its only exist in PHP and can be used as both get and posy methods.
Also Read : How to connect PHP to MySQL ?