PHP MySQL tutorial

GET and POST in PHP

GET and POST in PHP.

GET and POST are two different methods that are used to send information from the browser to the server. Browser sends information to the server via special encoding scheme called URL encoding. In this scheme information is sent to the server in the form of name and value pairs. Each pair is separated by the ampersand.Let’s have a detailed look at GET and MOST methods in PHP

GET Request

If the information that is to be send to the server is not very secure, you can use the GET method. The GET method appends the information at the end of the URL of the page to which information is being sent. To see how get method works, create a new file named Php-GET.php and paste the following code in it.

<html>
   <body>
      <form action = "get-output.php" method = "GET">
         Car: <input type = "text" name = "car" />
         Price: <input type = "text" name = "price" />
         <input type = "submit" />
      </form>      
   </body>
</html>

Download the codeRun the code

Now create another file named get-output.php and paste the following code in it.

<?php
   if( $_GET["car"] || $_GET["price"] ) {
      echo "The car ". $_GET['car']. "<br />";
      echo "costs ". $_GET['price']. " \$.";
      exit();
   }
?>

Download the code

Now if you open the Php-GET.php file in your PHP server and fill the car and price text boxes and then press the submit button, the GET method will be triggered and the server will redirect you to get-output.php page. At this moment, if you look at your URL bar, you will see that your URL will look like this “http://localhost:/get-output.php?car=toyota&price=1000000” the server name and the name of the car and price may differ according to the settings you have.

To send data via GET method, you simply have to set the value of the method attribute of the form to “GET”. PHP contains $_GET associative array which receives values passed as GET parameter. On get-output.php page this array is being used to display the name and price of the car.

POST Request

POST method is mostly used when you want to send sensitive information to the server. Unlike GET, the information being sent to the server is not appended Like GET method, you just have to specify POST as a value for method parameter of the form. PHP also contains a $_POST associative array which could be used to access values of the parameters passed to the page via POST request.

Create a file named Php.POST.php and add the following code in it:

<html>
   <body>
      <form action = "post-output.php" method = "POST">
         Car: <input type = "text" name = "car" />
         Price: <input type = "text" name = "price" />
         <input type = "submit" />
      </form>
   </body>
</html>

Download the code Run the code

Create another file named post-output.php in the same folder and copy and paste the following code in it.

<?php
	if( $_POST["car"] || $_POST["price"] ) {
		echo "The car ". $_POST['car']. "<br />";
		echo "costs ". $_POST['price']. " \$.";

		exit();
	}
?>
Download the code

Now if you open the Php.POST.php file, enter values in the text fields and then click submit button, you will be redirected to post-output.php page where you could see the values you passed in the text boxes on the previous page. POST and GET requests are used in conjunction with session and cookies for login and logout mechanism.

<<< Strings in PHPFunctions in PHP >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .