- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP GET and POST
There are two ways available in PHP, that the browser client can send the information to the web server, which are:
- GET Method
- POST Method
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.

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

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

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:

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

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

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 »
Follow/Like Us on Facebook
Subscribe Us on YouTube