$_GET

  • Description : The GET function is used to store data in the URL of a website and available for all to see. It is most often used when a form is submitted.
  • Language : PHP
  • Date Added : 14th October, 2009
Bookmark and Share

Welcome to another tutorial from ReadnTeach! First let's look at the basic structure of a form setup to use the GET function when submitted. For a more in depth look at forms click here. Here is the code for a basic GET form:

Code :
<form action="pageToSubmitTo.php" method="get"> Pet Name: <input type="text" name="petname" /><br /> Pet Age: <input type="text" name="petage" /><br /> <input type="submit" name="submit" value="submit" /> </form>
Output:
Pet Name:
Pet Age:

When setting up the form you must set the "action" to the page you want the information sent too. You can leave it blank by using two speech marks and it will submit to the page the form is on. The method can only ever be either "get" or "post". We're using get, to learn more about the POST function click here. When you submit the form the URL would look like this:

Output:
http://www.yourdomainhere.com/pageToSubmitTo.php?petname=lucky&petage=4

Now how do you access that data? Well by using the GET function. Below is some example code that could be used to retrieve and display the data submitted via the form:

Code :
<?php $petName = $_GET['petname']; $petAge = $_GET['petage']; echo "Your pet's name is " . $petName . "<br />"; echo "Your pet's age is " . $petAge; ?>

So if the user entered "lucky" and "4" into the form then the output would be:

Output:
Your pet's name is lucky Your pet's age is 4

To see the form working you can click here and try it out for yourself! You may be thinking why use GET? Well it allows a user to bookmark the page with the results of a form submission but it should never be used for storing sensitive information such as passwords. Well that concludes this tutorial, in the next tutorial we will look at using the POST function.



Login Required

To post comments you must sign in to your account below!

User :
Pass :

If you do not currently have an account click here to create one now!