The previous lesson of PHP Lessons Series was about PHP Functions and in this lesson we will learn about Get and Post in PHP.
Get and Post are HTTP Request methods used for communication between client and server. Client is a web browser requesting a response from the website for the action performed and server is the system where the website is hosted.
The above code checks if get request is not set then echo ('You have not made a get request') and if get request is set then echo ('You have made a get request'). If we simply execute this url (http://localhost/test.php) in our browser, we will get the response ('You have not made a get request') from the server as there is no paramter specified in the url call. But if we specify the parameter in the url call by executing the url given below:
http://localhost/test.php?parameter=yes
As we have set the parameter in the above url, so we will get ('You have made a get request') response from the server. The value of the parameter specified in the url can be retreived from the GET Array like this:
The above code contains an html form with an input text field (Name) and a submit button. The method of the form is POST which means that it will send data to server via POST method. The action of the form is (#) which means that the POST data will be submitted to the same server file as of the html form i.e (test.php). If we specify some other file in the form action, the POST data will be submitted to that specific file then. In the above example code the POST data submitted by the html form will be checked by the PHP code below and echo response message. If POST data is not set (i.e the page is just refreshed and the form submit button is not clicked), the response message will be ('You have not made a get request') and if the POST data is set (i.e the form submit button is clicked), the response message will be:
You have not made a get request
The name of user is John
Note: We can also make a GET Request to the server using html form method by keeping the method in the form = "GET".
Related PostsGet and Post are HTTP Request methods used for communication between client and server. Client is a web browser requesting a response from the website for the action performed and server is the system where the website is hosted.
GET() Request:
A GET() request is specified in the url. For example, we have a test file (test.php) on our local system containing the piece of code given below:
if(!isset($_GET['parameter'])){
echo 'You have not made a get request';
}
else if(isset($_GET['parameter'])){
echo 'You have made a get request';
}
The above code checks if get request is not set then echo ('You have not made a get request') and if get request is set then echo ('You have made a get request'). If we simply execute this url (http://localhost/test.php) in our browser, we will get the response ('You have not made a get request') from the server as there is no paramter specified in the url call. But if we specify the parameter in the url call by executing the url given below:
http://localhost/test.php?parameter=yes
As we have set the parameter in the above url, so we will get ('You have made a get request') response from the server. The value of the parameter specified in the url can be retreived from the GET Array like this:
$parameter = $_GET['parameter'];
POST() Request:
Unlike a GET Request a POST request is not sent in the url. We can make a POST request using curl, javascript or by submitting a simple html form. We will stick with the simplest method of making a Post Request i.e by submitting a simple html form. For explanation of a POST Request consider the example code given below:
<form method="POST" action="#">
Name: <input type="text" name="Name" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if(!isset($_POST['Name'])){
echo 'You have not made a get request';
}
else if(isset($_POST['Name'])){
echo 'You have made a get request<br />';
echo 'The name of user is '.$_POST['Name'];
}
The above code contains an html form with an input text field (Name) and a submit button. The method of the form is POST which means that it will send data to server via POST method. The action of the form is (#) which means that the POST data will be submitted to the same server file as of the html form i.e (test.php). If we specify some other file in the form action, the POST data will be submitted to that specific file then. In the above example code the POST data submitted by the html form will be checked by the PHP code below and echo response message. If POST data is not set (i.e the page is just refreshed and the form submit button is not clicked), the response message will be ('You have not made a get request') and if the POST data is set (i.e the form submit button is clicked), the response message will be:
You have not made a get request
The name of user is John
Note: We can also make a GET Request to the server using html form method by keeping the method in the form = "GET".
Session in PHP
Cookies in PHP
Include and Require Statements in PHP
How to get the ip address of user in PHP?
No comments:
Post a Comment