PHP – passing variables across pages Feb19 '05

While working in PHP, eventually you’ll want to be able to pass a variable, or piece of data, from one page... to another page.

After all, the power of variables are obvious on a single page – allowing great type–saving and re–use capabilties. But the true power of variables is their ability to be transferred to a completely different page, so they can be used in even more situations.

GET it from the URL

The quickest (but most limited) way to transfer variables is by a method called GET. With GET, you append the variables onto the URL of the page you want the variables to be transferred to:

http://www.matthom.com/contact.php?id=301&name=Matthom

The example above would give the contact.php page two variables to utilize: id, and name, whose values are 301, and Matthom, respectively.

You can add as many variables to the URL as you’d like.

Beware – sometimes you don’t want your variables to be shown "out in the open." Also, you are limited to 255 characters in the URL, so the variables can’t contain too much information.

From contact.php, you can GET these two variables via PHP:

# GRAB THE VARIABLES FROM THE URL
$id = $_GET['id'];
$name = $_GET['name'];

POST it from a FORM

Another way to transfer variables, and by far the more robust way, is to grab them from a form.

Let’s say this is your form field code:

<form action="process.php" method="post">
<input type="text" size="25" name="searchtype" />
<input type="text" size="25" name="searchterm" />
</form>

These two input boxes allow users to enter information. At process.php, you can grab the variables in the same way:

# GRAB THE VARIABLES FROM THE FORM
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm'];

Notice the use of $_POST[] over $_GET[]. This is an important distinction.

While using the POST method, there are no limitations on how many characters you can include. Also, the variables are not visible in the URL.

Categories: PHP , Web Development

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

Good article, but one clarification: there is a limitation on how many characters you can include when using POST (or there generally is), ... Read more.

Passing variables with $_GET and $_POST is good but only for certain things. I use $_COOKIE for storing details such as Name, Email, WWW in ... Read more.

Questions from a novice PHP user: Why is it that I can access values of variables on the page referred to in "action" on the form without m ... Read more.

In fact, $_POST['region'] does not seem to be carrying a value. If 'region' is the NAME ('name' att ... Read more.

Also a novice PHP user. I am trying to store an SQL statement across pages so I can reuse it at a later stage. I use this ... Read more.

what i’m trying to do is send a variable accross pages that can change values depending on some input like: ... Read more.

There is also another way to pass data through pages. You do this by using sessions. ... Read more.

I might seam unrelated but you are my best option... I’m trying to create an online job application, using FormMail 5.2 php scrip ... Read more.

after grabing some string values with quotes(') and double quotes(") from a variable using POST, and put them inside another form, like: ... Read more.

Kitradin, you have to use the "opposite" quote-type (single or double), when using quotes in attribute values: <input type="tex ... Read more.

what if they both have a single and double quotes? like bobby’s website: "the madman" ... Read more.

You have to escape either the single or double quotes, depending on what your default is: <input type="text" value="b ... Read more.

we are linked with other websites. Clicking on the link user starting our registration form. On thrid page of our registration form we have an opti ... Read more.

This is great feedback. Simple and to the point. It helped me fix a break in my PHP which calls a variable from a db. It was the only line missi ... Read more.

From a learning amateur PHP coder ... thank you! Your step by step guide explains everything nicely. ... Read more.

I would like pass the value without using post,session,through URL also to other pages. Can you give me idead about above criteria ... Read more.

Helping against injection - $id = (int)$GET['id']; cast value to int. $name = stripslashes($GET['name']); strip slashes placed there by ma ... Read more.

for security reasons, can a variable be passed on to a different page without using the GET method or a form or a cookie? cookies can be dangerous ... Read more.

Yes you can use PHP sessions (like server side cookies). more info here. ... Read more.

This helped me solve a problem I was struggling with. Thanks a ton. ... Read more.

Thank you! I couldn't remember how to pass the form value into a php file. ... Read more.

Thank you for your help! ... Read more.

Thank you very much, helped me a lot, nice info. ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

11894 unique visits since August 2008

Syndicate

Advertisements