Articles HTML Tutorial CSS Tutorial JavaScript Tutorial PHP Tutorial MYSQL Tutorial Contact Us
PHP GET & POST Arrays

So far, we've shown you how you can assign a value to a variable inside of PHP, and that's great if you know what that value should be.
But what if you want the value of a variable to be set by your website visitors, maybe based on something they select?
How do you pass a value from your HTML webpage to your PHP script?

There are two ways you can pass information to a PHP script... via a URL, or via HTML Forms.

In PHP, information obtained from a URL is known as GET data, and information obtained from a form is known as POST data.

Take a look at this HTML code:
Would you like a drink?<br>
<a href="phptest.php?drink=yes">Yes</a><br>
<a href="phptest.php?drink=no">No</a><br>
<br>
Would you like a drink?<br>
<form name="myform" method="POST">
<input type="radio" name="drink" value="yes">Yes
<br>
<input type="radio" name="drink" value="no">No
<br>
<input type="submit" name="submit" value="Submit Form">

The above code would produce a webpage that asks the question "Would you like a drink?", and provides you with 2 ways of saying either "yes" or "no".
You can either click on one of the links, or you can select one of the radio buttons and click the "Submit Form" button.

Clicking one of the links will send your preference (yes or no) to the PHP script via GET.
Selecting one of the radio buttons and clicking the submit button will send your preference to the PHP script via POST.

Which one of these methods you use, is upto you, and really depends on what kind of data you're sending. GET data is visible in your visitors address bar, and will be seen in their browser history. POST data is sent invisibly, and so is more secure. Someones username and password, for example, should be submitted by POST.

Ok, so how does your PHP script get the information from the GET or POST data?
PHP has 2 built-in Global Variables/Arrays called $_GET and $_POST that collect and store data sent to it via the GET or POST method.
You simply tell it which value you want:
<?PHP
$drink = $_GET['drink'];
?>
or <?PHP
$drink = $_POST['drink'];
?>
The first bit of the above code tells PHP to create a variable called $drink and give it the value of "drink" from the URL. So if the link your visitor clicked on included drink=yes, the value of "drink" is "yes", and so that's what's stored in $drink. In other words... GET the value of "drink" from the URL and store it in $drink.
The second bit of code does almost exactly the same thing, except it grabs the value of "drink" from the POST data, and stores it in $drink. In this case, "drink" refers to the name of the input element of the form.

There's another standard PHP Array that gets populated with both GET & POST data called $_REQUEST. If you're not sure how the data is going to be delivered to your script... maybe you have one script that is access via a URL aswell as by a form submittal (a search page for example), then you can check the $_REQUEST array instead: <?PHP
$drink = $_REQUEST['drink'];
?>


A special mention needs to be made regarding the submission of files.

If you have a form with a file input (<input type="file" name="myfile">), you cannot simply access it like any other $_POST variable with $_POST['myfile']. Instead, you must reference the PHP $_FILES array: <form>
<input type="file" name="myfile">
</form>

<?PHP
$myfile = $_FILES['myfile'];
?>

In addition, if you're uploading more than 1 file at a time, and want to include more than one <input type="file" name="myfile">, you need to make these input elements into an array: <form>
<input type="file" name="myfile[]">
<input type="file" name="myfile[]">
</form>

<?PHP
$myfile0 = $_FILES['myfile'][0];
$myfile1 = $_FILES['myfile'][1];
?>