What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server.
Two HTTP Request Methods commonly used for request-response between a client and server: GET and POST. In this article we are going to understand difference between Get and Post method in php.
The GET Method : Following example which shows you how to use get method.
http://www.example.com/test/demo_form.php?name1=value1&name2=value2
HTTP (Hypertext Transfer Protocol) retrieval method is mainly used on the client (browser) side to send a request to a specific server to retrieve specific data or resources. In this method, the server should only allow us to receive the data and not change its state. Therefore it is only used to see something and not to change it. The Get method is one of the most commonly used HTTP methods. The request parameter of the get method is added to the URL.
Get Prompt works best for data that doesn’t need to be secure (ie data that doesn’t contain images or Word documents).
Example: In the following HTML code, we have created a form with a text field such as firstname and lastname. We’ve also included a get.php php file to which our data will be sent after you click the submit button.
// index.html
<!DOCTYPE html>
<html>
<body>
<form action="get.php" method="GET">
First Name: <input type="text" name="firstname" /> <br>
Last Name: <input type="text" name="lastname" /> <br>
<input type="submit" />
</form>
</body>
</html>
In the following PHP code using the GET method we have displayed the First Name and Last Name.
// get.php
<!DOCTYPE html>
<html>
<body>
Welcome </br>
Your First Name is
<?php echo $_GET["firstname"]; ?> </br>
And Your Last Name is:
<?php echo $_GET["lastname"]; ?>
</body>
</html>
HTTP Post (Hypertext Transfer Protocol) method is mainly used on the client (browser) side to send data to a specific server to create or rewrite a specific resource/data. This data is sent to the server and stored in the request body of the HTTP request. Finally, the Post method results in creating a new resource or updating an existing one. Because of this dynamic usage, it is one of the most widely used HTTP methods.
The Post request works best for data that needs to be secure (i.e. data that contains images or text documents).
Example: In the following HTML code, we have created a form with a text field such as firstname and lastname. We’ve also included a PHP post.php file that our data will be sent to after you click the submit button. We are doing same example what we already did with get method. Now we are doing same with post method for better understand.
// index.html
<!DOCTYPE html>
<html>
<body>
<form action="post.php" method="post">
First Name: <input type="text" name="firstname" /> <br>
Last Name: <input type="text" name="lastname" /> <br>
<input type="submit" />
</form>
</body>
</html>
In the following PHP code using the HTTP POST method, we have displayed the First Name and Last Name.
// post.php
<!DOCTYPE html>
<html>
<body>
Welcome </br>
Your First Name is
<?php echo $_POST["firstname"]; ?> </br>
And Your Last Name is:
<?php echo $_POST["lastname"]; ?>
</body>
</html>
Introduction Git tags are an essential feature of version control systems, offering a simple way…
Introduction The methods that browsers employ to store data on a user's device are referred…
Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…
Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…
Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…
Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…