Scott Klarr Jr
Passing variable values in the url with php
PHP Tutorial: Passing Variables via URL
In PHP you have the ability to pass information between pages through the URL. This technique is extremely simple and great for things such as telling a script what bit of information is needs to load (example: a forum topic).
The format used to pass variables in the URL is the url itself, followed by a question mark, followed by the list of variables and variable data seperated by the amperand symbol. Example:
www.domain.com/index.php?variable1Name=theFirstValue&variable2Name=theSecondValue
Proper XHTML Coding
I should make a point right now that to be W3C valid in xhtml strict, you cannot have the & symbol anywhere in your code, instead, you must use & in the source code, which gets parsed as an amperand symbol by your browser. So if you wanted to link to the above URL example in html, the link code would look like:
<a href="www.domain.com/index.php?variable1Name=theFirstValue&variable2Name=theSecondValue">Link Title</a>
Accessing the variables in PHP
When a php page is loaded with properly formated variables in the URL, they can be accessed through the $_GET['variableName'] array in php. You simply replace variableName and you can access the data for the specified variable. Using the above URL example, lets see how php can access and output the data:
<?
$v1 = $_GET['variable1Name'];
$v2 = $_GET['variable2Name'];
echo "Variable One Value: $v1
Variable Two Value: $v2
";
?>
Creating url variables with php
When creating a script that will be passing data through the URL, you have to keep in mind the possibility of characters causing parse errors in the URL. The best way to overcome such problems is to use the urlencode() PHP function. This will take a string of data and return it with all special characters trasformed into URL-friendly entities.
<a href="index.php?variableName=<? echo urlencode(" 'hello & goodbye' "); ?>"></a>
The above value of 'hello & goodbye' will then be transformed into +%27hello+%26+goodbye%27+ making it completely safe to use within a URL.
This topic has the following tags:
Last 5 Linkbacks
- Jan 10, 2010www.v7n.com
- Dec 10, 2008mercadoformosa.com.ar
- Feb 05, 2008phpnet.agendamilano.info
- Jan 28, 2008php.coding-school.com



Jordan McClements Sep 10, 2008
Perfect!
This is exactly what I was looking for (without having to wade through php reference books).
Thanks!
edu Sep 28, 2008
perfect
JayGee Jan 22, 2009
Thanks mate, you're a diamond! That is exactly what I was looking for!
Sina.Giant Feb 08, 2009
Thank you so much, Exactly what I was searching for, saved me big time...gj!
shermi Feb 13, 2009
I want more help.I want to pass values to url without any user interaction. How can I do?Please help.
jack May 01, 2009
send me the session handling that's how to access the values from the web 3 and also give me the mail code i done mail() function but this function not works.
nilmini Dec 22, 2009
thanks buddy
wiyono Jan 25, 2010
i don't understand please help meee...
panik.
devdas Jan 25, 2010
how to send post value like this ......
u explained about GET value..
In post ,the URL doesn't show the values....
i want to send link
index.php?value=1 using GET method.
simply $_GET['value'];
index.php using POST method.
like wise $_POST['value'];
this is ok......im asking..How to send the value in url (while using POST Method).
Thanks....
Xing Jan 28, 2010
Thank you! This article has helped me and has solved my problem!