Loading
Cancel
Webmail Login
Control Panel Login
php Passing Variables between Pages via $_GET
.CO.UK ONLY £5.33
Find your perfect domain here

php Passing Variables between Pages via $_GET

php pass data via get

We often get asked how to pass information from one page to another in php. Using the GET function this guide will show you how to pass small but vital information via the address bar.

The first thing you will need to do is to insert the following link in the page you want to pass the information on from. In this guide we will pass 'name' over a link to a page called portfolio.php.

<a href="http://www.yourdomain.co.uk/portfolio.php?name=jonathan">

then in portfolio page (or the page you want to collect the data in) put the following code:

<?php
  $name = $_GET['name'];
  echo $name ;
?>

Code Breakdown

So what has the above done? It has passed the value 'jonathan' over to the next page. Below is a break down of what each bit of code does.

?name=jonathan

This can be altered to anything you like. For example if you wanted to fetch some information from a database and needed to pass the ID to the next page you could use ?id=1. To pass a colour across you would use ?colour=blue.

The next part of the code collects the information from the address bar after the user has clicked the link.

<?php
  $name = $_GET['name'];
  echo $name ;
?>

The line

  $name = $_GET['name'];

is saying what the variable is called ($name) and the second part is saying fetch me the data in the url that corresponds to the variable called name.

  echo $name ;

The second bit is saying display the variable we collected from the address bar.

So using our colour example we would change the code to this:

<?php
  $colour = $_GET['colour'];
  echo $colour ;
?>

Remember this will only work on php pages not html htm pages etc and no security has been added (e.g. mysql_real_escape_string or strip_tags).

 



Leave a comment

Has this guide helped you? Have an idea for a guide or need help? Let us know below.

© Copyright 2014 | Squirrel Hosting Limited
Company Number 9031308