Home » Programming, Scripts, Discussion

A quick and easy way to pass PHP variables between pages

3 February 2009 121 views One Comment

We know this post is a little off topic considering it doesn’t pertain to average designer, but it certainly is web related so we figured we would share it. There are many ways to pass variables between pages whether your writing the variable to a session, passing arrays between pages, passing the variables in links, or using the method that we will describe below which is using the post method. This example should be relatively easy to follow for even the true beginners. The source files used in this example are available for download (free) at the bottom of this post.

Step 1. Create index.php
Using any code/text editor, create a file “index.php”. This index.php file will contain the form we are going to use. Add your form fields (as shown in the screenshot).
Create Index File

Step 2. Add a name to each field
Now you are going to add a unique name to each field so we can identify them when processing the form.
Label each field with individual name

Step 3. Create field_process.php
Now we will assign a corresponding variable to the variables that we have sent post from the previous page. At this point we’ve finished the task of passing the variables from one page to another.
Create Field Process

Step 4. Show the results on the field_process.php page
Now we are going to take it a step further and show the posted results on the field_process.php page using the echo function.
Show the results

DOWNLOAD SOURCE:

Download File

No related posts.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

One Comment »

  • Fiji Web Design said:

    Great tutorial. In practical implementation you’ll have to make sure the variables you echo to HTML are HTML entity encoded.

    eg:

    Character encoding has to also be taken into consideration.

    eg:

    This makes sure the htmlentities are UTF-8 encoded and thus further prevents exploits based on encoding.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.