By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,160 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

How to pass variables via GET

P: 4
Hello I have been wondering How do web developers do this "index.php?option=..." the url has a question mark at the end and some function?!? Do you have any links for tutorials that can guide me in learning this?!?
May 22 '07 #1
Share this Question
Share on Google+
6 Replies

P: 79
Is the GET method.
You have the chance to use 2 methods:

GET: the var value is passed directly in the URL after a ?

POST: the var is invisible in the URL (used exspecially with passwords...)

You can chose the method in the form adding the method propriety and then adding ='GET' or ='POST'
PHP sintax to get vars in GET is:
PHP sintax to get vars in POST is:

Kind Regards
May 22 '07 #2

Expert 5K+
P: 5,058
This is called the HTTP GET protocol.
It is used to transfer data between pages, but being a pretty old protocol, it has limitations, such as length restrictions (due to URL length restrictions) aswell as character restrictions. (# and other special characers are not available using GET)

You can add GET variable on any URL by adding a ? sing at the end of the URL. Then you can add variables formatted like this "varName=varValue". To add more than one you can seperate them usint a & sign.
Example: index.php?var1=value1&text=Thisistext1

Now you could access those variables in PHP using the $_GET array
Example using the URL in the previous example.
Expand|Select|Wrap|Line Numbers
  1. print $_GET['var1']; //Outputs: value1
  2. print $_GET['text']; //Outputs: Thisistext1
You can also access this data usint the $_REQUEST array. This array is different however. It does not only contain GET data. It also containts POST and COOKIE data. Depending on your PHP settings, identical variables in the other protocols might override your GET values.

Here are some examples of how GET values can be set.
Expand|Select|Wrap|Line Numbers
  1. // This link will refresh the page and change the
  2. // GET variable text
  3. echo "<a href='?text=fromalink'>Linkage</a>";
  5. // This form will post its text field as the
  6. // GET variable text
  7. ?>
  8. <form action="?" method="GET">
  9.   Text: <input type="text" name="text" />
  10. <br /> <input type="submit" text="Submit" />
  11. </form>
  12. <?php
May 22 '07 #3

P: 4
I see so that's how it is... I will try this method then... Thanks... If I have more questions regarding this I will just post it here then... Thank You!


How would I use the GET method to link to another page?!? for example I have an index.php file and a test.php file.... in my index.php file there should be a link to open the test.php file... How will I use the GET method in order for it to link to the test.php file?!?? In order for me to have a ""
May 23 '07 #4

Expert 5K+
P: 5,821
Changed thread topic to better match contents.
May 23 '07 #5

Expert 100+
P: 1,584
echo '<a href="header.php?page=test1">Page 1</a>';
echo '<br>';
echo '<a href="header.php?page=test2">Page 2</a>';


$link = $_GET['page'];

put these two files with another two files named test1.php and test2.php with the same dir and execute index.php
May 23 '07 #6

P: 4
Thank You for the reply... I will try this then.
May 23 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.