How to get POST request parameters in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions.

Note: Page URL and the parameters are separated by the ? character.

parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it. It parse an URL and return an associative array which contains its various components.

Syntax: 

parse_url( $url, $component = -1 )

parse_str() Function: The parse_str() function is used to parse a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.

Syntax:  

parse_str( $string, $array )

Approach: Parse the URL string using parse_url() function which will return an associative array that contains its (passed URL) various components. The query of the array returned by parse_url() function which contains a query string of URL.

Below examples uses parse_url() and parse_str() function to get the parameters from URL string. 

Example 1:  

PHP




<?php

 

// Initialize URL to the variable

$url ='https://www.geeksforgeeks.org?name=Tonny';

parse_str( $string, $array )

parse_str( $string, $array )
1

parse_str( $string, $array )
2

parse_str( $string, $array )
3

parse_str( $string, $array )
4 =
parse_str( $string, $array )
6
parse_str( $string, $array )
7$url
parse_str( $string, $array )
9

 

Hi Tonny
0

Hi Tonny
1

Hi Tonny
2
parse_str( $string, $array )
7
parse_str( $string, $array )
4
Hi Tonny
5
Hi Tonny
6
Hi Tonny
7
Hi Tonny
8
parse_str( $string, $array )
9

parse_str( $string, $array )

Hi Amit your emailID is [email protected]
1

Hi Amit your emailID is [email protected]
2
Hi Amit your emailID is [email protected]
3
Hi Amit your emailID is [email protected]
4
Hi Tonny
8
Hi Tonny
5
Hi Amit your emailID is [email protected]
7
Hi Amit your emailID is [email protected]
8

 

Hi Amit your emailID is [email protected]
9

Output: 

Hi Tonny

 

Example 2: 

PHP




<?php

 

// Initialize URL to the variable

$url =<?php4;

parse_str( $string, $array )

parse_str( $string, $array )
1

parse_str( $string, $array )
2

parse_str( $string, $array )
3

parse_str( $string, $array )
4 =
parse_str( $string, $array )
6
parse_str( $string, $array )
7$url
parse_str( $string, $array )
9

 

Hi Tonny
0

Hi Tonny
1

Hi Tonny
2
parse_str( $string, $array )
7
parse_str( $string, $array )
4
Hi Tonny
5
Hi Tonny
6
Hi Tonny
7
Hi Tonny
8
parse_str( $string, $array )
9

parse_str( $string, $array )

Hi Amit your emailID is [email protected]
1

Hi Amit your emailID is [email protected]
2
Hi Amit your emailID is [email protected]
3
Hi Amit your emailID is [email protected]
4
Hi Tonny
8
Hi Tonny
5
Hi Amit your emailID is [email protected]
7=4=5
Hi Amit your emailID is [email protected]
4
Hi Tonny
8
Hi Tonny
5=9
Hi Amit your emailID is [email protected]
8

 

Hi Amit your emailID is [email protected]
9

Output: 

Hi Amit your emailID is [email protected]

 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

How to get POST params in PHP?

PHP POST request php $name = $_POST['name']; if ($name == null) { $name = 'guest'; } $message = $_POST['message']; if ($message == null) { $message = 'hello there'; } echo "$name says: $message"; The example retrieves the name and message parameters from the $_POST variable. We start the server.

How to get POST data value in PHP?

In PHP, we can use the $_POST method as a superglobal variable that is operated to manage form data. After we click on submit button and the page will send the data through the post method. We can use the data after storing it in a variable according to our requirements.

How to get parameter value in PHP?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

What is GET POST and request variables in PHP?

Get and Post methods are the HTTP request methods used inside the <form> tag to send form data to the server. HTTP protocol enables the communication between the client and the server where a browser can be the client, and an application running on a computer system that hosts your website can be the server.