Grabbing $_GET
Posted on 12. Mar, 2010 by admin in Basics
Ah, another one of those choose one or the other they both do the same thing. We can use the same HTML code, but change the method to GET.
<html> <head> <title>Flipping Forms Tutorial</title> </head> <body> The following is a form: <br/> <form action="index.php" method="get"> What is your favorite book?: <input type="text" name="book" /><br/> What is your favorite movie?: <input type="text" name="movie" /><br/> What is your favorite website?: <input type="text" name="website" /><br/> <input type="submit" /> </form> </body> </html>
Now the difference between GET and POST is a matter of visibility. When we hit the submit the POST data “magically” goes to the next page. With GET the data is put on the end of URL. When you hit submit you should see something like this on the end of your URL ?book=Harry+Potter&movie=Star+Wars&website=WildPHP+of+Course! .To print out the variables like last time we just use $_GET instead of $_POST:
Printed Results:
<?php
//Print out our GET Data
echo “Favorite Book: “.$_GET["book"];
echo “Favorite Movie: “.$_GET["movie"];
echo “Favorite Movie: “.$_GET["website"];
?>
Sample Output:
Favorite Book: Harry Potter
Favorite Movie: Star Wars
Favorite Website: WildPHP of Course!
Now that the data is viewable in the URL it probably won’t be the best idea to send passwords or sensitive data via GET. GET however can be very useful because we can pass information to PHP without having to fill out a form. If you look above at the URL you are at right now you can see GET in use. Using the info in the URL, PHP knows what post to pull up.
If you want to create a GET variable in a URL just follow this simple format: ?varname=value&varname2=value2. Notice that it always starts with a question mark, and additional values can be added with an ampersand(&).

