RSS

GET variables

Mon, Jan 18, 2010

PHP & MySql

A second method to send user filled in data (input) from forms or just to send data through the url to another page, is the GET method. The GET method works through the url of the page, and defines the GET variables inside the url.

Example:

Url: webcodez.net/showthread.php
GET variables: none
Url: webcodez.net/showthread.php?p=23
Get variables: $_GET['p'] = 23

You see you can send GET variables through the url to a page by adding ?get_variable_name=value to the url. The GET variables given through the url will be stored in an array $_GET, which contains all GET variables like: $_GET['get_variable_name']. In the above example we set a GET variable we named ‘p’, so it will be stored in $_GET['p'], and we gave it the value 23, so it will become $_GET['p'] = 23. You can however also add more GET variables through the url, but therefore you need to use the & sign.

Example:

Url: webcodez.net/editpost.php?do=updatepost&postid=22
GET variables: $_GET['do'] = “updatepost” and $_POST['id'] = 22

In these examples the GET variables are used to define what thread of the forums to show, or what thread to edit. GET variables can so be very usefully when one page is used to complete an action that can be used for multiple things ( in this case multiple threads ).
Let’s make our own file that shows all GET variables:

File: getvars.php

<?php
foreach($_GET as $name => $value) {
"GET variable '".$name."' = ".$value."\n";
}
?>

This file simply shows all GET variables, showing their names and values. You can try it out by opening the file. One example of the output when giving it some GET variables through the url.

Example:

Url: getvars.php?a=10&b=4&c=7
GET variables: $_GET['a'] = 10, $_GET['b'] = 4 and $_GET['c'] = 7
Output of the file:
GET variable ‘a’ = 10
GET variable ‘b’ = 4
GET variable ‘c’ = 7

Cheers,
Admin.