codescracker


php

PHP GET and POST



« Previous Tutorial Next Tutorial »


There are two ways available in PHP, that the browser client can send the information to the web server, which are:

Before, the browser sends the information to the web server, first it encodes using a scheme called URL encoding like this:

name1=value1&name2=value2&name3=value3

PHP GET Method

PHP GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the question mark (?) character. Here is an example:

https://codescracker.com/form_submit.php?name1=value1&name2=value2

Where to use GET Method

GET method is used to pass information which are not sensitive. When you submit form that uses the GET method, then on submitting the form, the entered information appends to the url and users can see that passed information from the URL. It means that the information is not encrypted.

In simple word, just use GET method to get or retrieve data from the database.

PHP GET Method Example

Here is an example, uses GET method to submit a form filled and submitted by a user in PHP:

<?php
	// this code will check if form is submitted using 
	// get method or not
	if($_SERVER['REQUEST_METHOD'] == "GET")
	{
		// this code will check that both name and city 
		// is entered or not
		if(isset($_GET['name']) && isset($_GET['city']))
		{
			echo "You are ";
			echo $_GET['name'];
			echo " from ";
			echo $_GET['city'];
			exit();
		}
	}
?>
<html>
<head>
	<title>PHP GET Method Example</title>
</head>
<body>
<form method="GET">
	Name: <input type="text" name="name"><br/>
	City: <input type="text" name="city"><br/>
	<input type="submit">
</form>
</body>
</html>

If you are submit the form data to the same page, then don't use action attribute to the form as in the above program.

Here is the sample output produced by the above GET method example code in PHP.

php get method

Now enter the information, that is, name and city, as shown in the following figure:

php get method example

Now click on the Submit button, then the output will become like this:

php get

PHP POST Method

The POST method transfers information via HTTP headers.

Where to use POST Method

POST method can be used anywhere, either for sensitive information or non-sensitive information as this method provides a secure method to submit any form to the web server.

In simple word, use POST method to feed or enter user data into the database.

PHP POST Method Example

Here is an example, uses POST method to submit a form filled and submitted by a user in PHP:

<?php
	// this code will check if form is submitted using post 
	// method or not
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		// this code will check that both name and city 
		// is entered or not
		if(isset($_POST['name']) && isset($_POST['city']))
		{
		echo "You are ";
		echo $_POST['name'];
		echo " from ";
		echo $_POST['city'];
		exit();
		}
	}
?>
<html>
<head>
	<title>PHP POST Method Example</title>
</head>
<body>
<form method="POST">
	Name: <input type="text" name="name"><br/>
	City: <input type="text" name="city"><br/>
	<input type="submit">
</form>
</body>
</html>

Here is the sample output produced by the above PHP post method example code:

php post method

Now enter the information, that is, name and city like this:

php post method example

Now click on the Submit, then the output will become like this:

php post

Difference between GET and POST Method

When user submit normal information to the web server, then use GET method. And when user submit private information or important information to the web server like password, email id, bank account details, then only use POST method there.

In simple word, GET method is used to get or retrieve data from the database whereas POST method is used to post or feed data into the database.


« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test