HTTP methods GET and POST

The two most used HTTP methods are: GET and POST. The PHP superglobals $_GET and $_POST are used to collect form-data.
What is HTTP?
The Hypertext Transfer Protocol (HTTP) is planned to enable interactions between clients and servers.
HTTP works as a request-response protocol between a client and server.
A web browser may be the client, and an application on a computer that hosts a web site may be the server.
Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.
Two HTTP Request Methods: GET and POST
Two commonly used methods for a request-response between a client and server are: GET and POST.
  1. GET - Requests data from a specified resource
  1. POST - Submits data to be processed to a specified resource
In GET method associative array of variables passed to the current script via the URL parameters.
create and index.php file inside your root directory and put the given code.

PHP Code:
  1. <?php
  2.  
  3.  $name = $_GET['name'];
  4.  
  5. echo "Name is ".$name;
  6.  
  7. ?>
now open web browser and write localhost/index.php?name=Your Name



Now lets talk about HTTP POST method in php. HTTP POST requests deliver additional data from the browser to the server in the message body. In contrast, GET requests consist of all required data in the URL. Forms in HTML can use either method by specifyingmethod="POST" or method="GET" (default) in the <form> element. The method specified determines how form data is submitted to the server. When the method is GET, all form data is encoded into the URL, appended to the action URL as query string parameters. With POST, form data appears within the message body of the HTTP request.

First of all make a html form in index.php file that we recently created.

HTML Code:
  1. <html>
  2. <head>
  3.            <title>intphp.blogspot.com</title>
  4. </head>
  5. <body>

  6. <form method="post" action="index.php" >
  7. <table border="1">
  8.  <tr>
  9. <td>Enter Name :</td><td><input type="text" name="name" ></td>
  10. </tr>
  11. <tr>
  12. <td colspan="2"><input type="submit" name="submit" value="Submit Form"></td>
  13. </tr>
  14. </table>
  15. </form>
  16.  
  17. </body>
  18. </html>

Form will look like the given form:

Enter Name :

Now write the php code at anywhere in index.php file:
HTML Code:
  1. <html>
  2. <head>
  3.            <title>intphp.blogspot.com</title>
  4. </head>
  5. <body>

  6. <form method="post" action="index.php" >
  7. <table border="1">
  8.  <tr>
  9. <td>Enter Name :</td><td><input type="text" name="name" ></td>
  10. </tr>
  11. <tr>
  12. <td colspan="2"><input type="submit" name="submit" value="Submit Form"></td>
  13. </tr>
  14. </table>
  15. </form>
  16.  
  17. </body>
  18. </html>
  19. <?php 
  20.  
  21.  if(isset($_POST['submit'])){
  22.  
  23. $name = $_POST['name'];
  24.  
  25.  echo "Name is : ".$name;
  26.  
  27.  }
  28.  
  29. ?>
When you will press Submit Form button after Entering name in text field then php code will be executed. you will see the result in your browser. You may also can use $_GET method in form.