Greetings,
Ok I think the above is a little confusing... so.
$_GET and $_POST are used to obtain information passed to a webpage.
How this information is passed determines which one you use. So in your example you're using a form so you would use the $_POST array and then set the method to post for your form like so;
Code:
<form name="my_form" method="post" action=" ... ">
where the 'action' is the URL of the file that will handle the data. You could also use 'get' as the form method but it's normally set to 'post'.
Copy the following and save it as 'test_form.php' where you can run it from via a browser;
Code:
<?php
if($_POST['colour'] == 'red')
{
print '<p style = "color:red;">Red is your favourite color</p>';
}
elseif($_POST['colour']== 'yellow')
{
print '<p style = "color:yellow;">Yellow is your favourite color</p>';
}
elseif($_POST['colour'] =='green')
{
print '<p style = "color:green;">Green is your favourite color</p>';
}
elseif($_POST['colour'] == 'blue')
{
print '<p style = "color:blue;">Blue is your favourite color</p>';
}
else
{
print '<p class = "error">Select your favourite color</p>';
}
if( isset($_POST['submit']) )
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
echo '<form method="post" action="test_form.php">';
echo '<select name="colour" size="1">';
echo '<option value="0">Select Colour</option>';
echo '<option value="red">Red</option>';
echo '<option value="yellow">Yellow</option>';
echo '<option value="green">Green</option>';
echo '<option value="blue">Blue</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="submit" />';
echo '</form>';
?>
when ran for the first time you'll get 4 notices about an undefined index 'colour' which is because as the form has not yet been submitted the index 'colour' does not exist in the $_POST array.
Select a colour from the list and click Submit you'll then be shown a message and also the contents, data passed from form, of the $_POST array.
$_GET is normally used to obtain data passed via a URL such as;
http://localhost/test.php?key=value&colour=red
in the above example you'd use something like the following to obtain the passed values;
$key = ( isset($_GET['key']) ? $_GET['key'] : '');
$colour = ( isset($_GET['colour']) ? $_GET['colour'] : '');
$food = ( isset($_GET['food']) ? $_GET['food'] : '');
$key would then contain 'value' and $colour would contain 'red' and $food would be an empty string as it's not in the URL.