PHP Get and Post: Getting URL Parameters and Form Data in PHP

There are two ways to submit data directly to a CGI program: GET and POST.

GET data consists of parameters specified in the URL. HTML forms submit data like this when the "get" method is specified in the form.

POST basically means any method of sending data that isn't a simple GET.

Let's take a look at a PHP script that retrieves GET data and displays the results as HTML.








PHP Get: Retrieving Data in PHP Using the GET Associative Array



'Get' data appears in your PHP script in an associative array called $GET.

The following script shows how you can retrieve particular parameters using $
GET; we also iterate through the $_GET array and show all parameter values.

<html>
<head><title>PHP Get Results</title></head>

<body>

<?php

// Show all URL parameters (and
// all form data submitted via the 
// 'get' method)
foreach($_GET as $key=>$value){
    echo $key, ' => ', $value, "<br/>n";
}

// Show a particular value.
$id = $_GET['id'];

if($id) {
    echo '<p/>ID: ', $id, "<br/>n";
}
else {
    echo '<p>No ID parameter.</p>';
}

?>

</body>

</html>




If we access the above page with the following url (with the script running on localhost)

http://localhost/test.php?id=goodbye&mode=run

.... we get the following HTML output.

id => goodbye
mode => run

ID: goodbye




Submitting GET Data With HTML Forms



We can also invoke the above script using a form in HTML. Here's a simple HTML form. Note that the form 'method' is set to 'get'.

<html>
<head>
<title>A Simple Form</title>
</head>

<body>

<form action="/test.php" method="get">

<input type="text" name="input" />
<input type="submit" value=" OK " />

</form>

</body>

</html>





When we access this form in a browser, we get a text box and a button. We'll type 'hello' in the text box and click OK:



Our PHP script now displays this:

input => hello

No ID parameter.




The value of the form text box (named 'input') has appeared in the $_GET associative array.

If you want to send a lot of data in a form or you don't want form data appearing in the URL, you should use POST instead of GET.

PHP Post: Retrieving POST Data With PHP



Post data appears in your PHP script in the $_POST associative array.

In the following HTML form, I've changed the 'get' method to 'post'. I've also added a hidden input control named 'id'.

<html>
<head>
<title>A Simple Form</title>
</head>

<body>

<form action="/test.php" method="post">

<input type="hidden" name="id" value="1234" />
<input type="text" name="input" />
<input type="submit" value=" OK " />

</form>

</body>

</html>




I've changed test.php to look at the $POST associative array instead of $GET.

<html>
<head><title>PHP Get Results</title></head>

<body>

<?php

// Show all URL parameters (and
// all form data submitted via the 
// 'get' method)
foreach($_POST as $key=>$value){
    echo $key, ' => ', $value, "<br/>n";
}

// Show a particular value.
$id = $_POST['id'];

if($id) {
    echo '<p/>ID: ', $id, "<br/>n";
}
else {
    echo '<p>No ID parameter.</p>';
}

?>

</body>

</html>





Let's type dandelion into the HTML form and click OK.



The new PHP script displays this:

id => 1234
input => dandelion

ID: 1234