tutorial.jcwcn.com home / Web Design & Development / PHP / Introduction to PHP > text Go back Print

URL Query String Basics

  2009-05-08 10:52:38  

URL Query String Basics

Have you noticed an URL with a question mark following the filename? Most probably you have. This is a technique used to pass data to web pages using URL query strings. What is a URL query string?

URL Query String is that portion of the URL that contains data to be passed to a web application. Simply put, it's the portion of the URL following the question mark. For example, on "index.php?pageid=7&username=Leo" the URL query string would be "pageid=7&username=Leo".

You can send data on a query string in two ways:

  • on a hyperlink
  • with a HTML form



Passing a query string with a hyperlink

<a href="index.php?username=Leo&location=Texas">click me</a>



Passing a query string with a HTML form

<form method="GET" action="index.php">
Enter username: <input type="text" name="username" /><br />
Enter location: <input type="text" name="location" /><br />
<input type="submit" value="submit">
</form>



If you enter "Abraham" on the username field, "Austin" on the location field and click the submit button, you will be taken to the following URL: index.php?username=Abraham&location=Austin

When somebody clicks this link

<a href="index.php?username=Leo&location=Texas">click me</a>



Two variables will be sent: username and location. The query string it's the portion following the question mark and the variables are separated by an ampersand (&). You can pass more variables if you wish by separating each variable name/value pair from the next with an ampersand. For example: index.php?username=Leo&location=Texas&style=green&blogname=CodeRemix

How to access query string variables using PHP


<a href="index.php?username=Leo&location=Texas">click me</a>



There is a variable named username and its value is "Leo". Another variable name is location and its value is "Texas". After you click the above link, you can access this data at index.php. For example, you could display the contents of the $_GET variables like this:

echo 'Welcome '.$_GET['username'].' from '.$_GET['location'];



Which would display "Welcome Leo from Texas".



/Web-Design/PHP/Introduction-to-PHP/2009-05-08/13866.html