Using PHP to Send Data Between Webpages

use php to pass data between web pages

Every now and again we web designers need to pass user input between pages. It could be a username or affiliate code that needs adding into a set of links; it could be details of a contact form that need passing over to a confirmation page before being sent to a database or email server; or it could be any other bit of information that a site visitor enters into one form that must be reproduced or used somewhere within another page or set of pages. Luckily, for those of us who haven’t studied PHP, the recipe for passing information between pages contains few ingredients and is easy to follow and doesn’t require any real knowledge of PHP.

This short tutorial will take you through the process of collecting information from visitors and sending that information to another webpage for further usage or display. We will use two recipes:

  1. The first recipe uses only one PHP page and will help you see how the process works. It will not be explained in detail.
  2. The second recipe is split over two pages and will be explained in detail.

The Ingredients

  • 1 HTML Page
  • 1 PHP Page
  • 3 Dashes of HTML
  • <p>
  • <form>
  • <input>
  • 2 Dashes of PHP
  • ECHO
  • $_REQUEST
  • Internet Server Space or a PHP Environment (you could use LAMP)

One note of caution: the code I’m about to show is not built with security in mind so if security is a concern then you will need to encrypt and sanitize the user input.

First the recipes and then an explanation of the ingredients.

Recipe One: Putting Everything On One Page

  1. Create a php page called test.php
  2. Put the following lines of code into it
  3. Fill out the details for $NAME, $AGE and $HOME with your name, age and hometown then save the page before opening it in a web browser.

Quick Explanation:

When you view the page you will see that $NAME, $AGE and $HOME between the <p> and </p> elements are replaced with the details you placed between the quotation marks. For example in $NAME=”ENTER NAME HERE”, anything that replaces “ENTER NAME HERE” will replace $NAME.

$NAME, $AGE and $HOME are called variables and can be assigned any single character or character string value. The echo command prints data to the screen. Our echo command is special because it has a start and end point as determined by >>>TEXT and TEXT, respectively. Whatever is written between those start (>>>TEXT) and end (TEXT) points is echoed to the screen. Variables like our $NAME, $AGE and $HOME are replaced with their assigned values when echo prints them to the screen.

Recipe Two: Passing information from one page to another page

  • Create an html page called page-one.html,
  • Put the following lines of code into it
  • Create a PHP page called page-two.php
  • Put the following lines of code into it
  • View page-one.html in your web browser, fill in the requested details then press send.

Explanation

As happened in Recipe One, $NAME, $AGE and$HOME are replaced by the data they represent but this time their assigned values come from the details entered into the form on page-one.htmlpage-one.html collects information and passes it to page-two.php . This is done in four steps:

  1. The <form> element has an Action and Method parameter e.g <form id=”FormOne” action=”page-two.php” method=”post”>
    • Action points to the page where the data collected by the form will be sent to be processed
    • Method tells the browser how to send the collected data. It takes one of two values: get or post
      1. get sends data via the URL
      2. post sends data via HTTP
    • Anything sent by get is visible in a browser’s address bar (i.e within the URL) whereas anything sent via post is only visible to hackers. Get can only handle limited character strings i.e only characters that can be embedded within a URL and only as many characters as a URL can carry. Conversely, post has no such character limitations. More often than not, post is the best method to use when sending data between pages.
  2. Every <input> element has a name attribute which identifies the data entered into its form field so it can be requested by page-two.php e.g <input type=”text” name=”Name” />
  3. When the “Send” button on page-one.html is activated the data from the form is posted to page-two.php where it is requested by name using the $_REQUEST['name'] command and is assigned to a variable with an equality (=) symbol. For example the instruction $NAME=$_REQUEST['Name'] assigns the variable $NAME with the value entered into <input type=”text” name=”Name” />
  4. The data is then printed to the screen using the echo command to display whatever is placed between >>>TEXT and TEXT and the variables between those delimiters are swapped for whatever values they have been assigned.

The $_REQUEST command can be replaced with $_GET or $_POST. You would use $_GET when the form method is get and $_POST when the form method is post. $_REQUEST catches information sent either way and is useful for lazy people like me.

Working example of Recipe Two

How To Use It

For those who have no understanding of PHP and who do not want to know how it works

First, in the HTML page, create an input field by using the HTML <input /> element and give it a unique name. Put the <input /> element between the <form></form> tags. For example, Name a fruit: <input type=”text” name=”fruit” />

Next, create a variable (like $NAME) between <?php and echo in the PHP page and assign it a value by relating it to a corresponding unique name from one of your <input /> elements. For example, $FRUIT=$_REQUEST['fruit'];

Lastly, call it by placing it in whatever text or HTML is put between the echo <<<TEXT and TEXT statements (a variable is like a pronoun, it stands in for something else. All variables begin with a dollar ($) sign). For example, My favorite fruit is $FRUIT

Be aware that PHP differentiates between lowercase and uppercase letters so fruit is not the same as FRUIT hence I’ve been able to use the word “fruit” twice in the above examples to mean two different things.

Safety First (or second, or last)

A few notes of caution on PHP usage:

You should never trust the data entered by a site’s visitor – treat it as you would a plague, check that the data entered fits the format required by the form field. For example, a telephone number will always contain numbers, could contain a dot or plus sign so strip it of any other characters;

When you use a double-quote () within an echo statement, you should place a backslash ( ) in front of it otherwise you will get an error message instead of your intended text message. For example

echo “this is a “double quote” within an echo statement”, should be written

echo “this is a “double quote” within an echo statement”

Further Reading

More informtion about the HTML tags and PHP commands used in this article can be found by clicking these links:

  Copyright secured by Digiprove © 2010

Comments

Leave a Reply