indiWiz.com
SiteSearch:
Jump:

Site Map | WizTools.org | jCraze Blog
Web Developer's Den!
Home : WDD : ServerSide
This Article...
Print Version
Add Comment
View Comments
ServerSide
Apache on Linux
Apache User Directory Configuration
PostgreSQL Configuration
CGI: What and How?
PHP Jump Start Guide
Arrays in PHP
PHP and HTML Forms
Variable Passing in PHP
PHP 'include'
Drop-down Menu Navigation in PHP
Edit Your Site Online Using PHP
PHP MySQL Polling System
Dynamic, Search Engine Friendly Pages
Quot Ticker Using SSI
Regular Expressions Reference
PHP File Browser
PHP DB Deletion/Approval Script
Sections

Commniquè
Sign Guestbook!
Read Guestbook!
MailMe!

Passing Variables to PHP Scripts

We will be discussing how to pass a variable from a HTML file to a PHP script. Basically this can be done in two ways:

  1. Using HTML forms. This has been discussed in my previous article.
  2. Using Link <A> Tag. If you had noticed closely, when using 'get' as the value of the attribute method in a form, the variables in the form are passed on as URL to the calling script. We will be using this technique in the <A> tag also. I will explain the concept with an example.

Example

Files:

  1. bgchooser.html
  2. bgprocess.php

What it does?

The bgchooser.html has three links:

  • Red Background with Black Text
  • Green Background with Red Text
  • Blue Background with White Text

When a particular link is clicked, the file bgprocess.php is opened with the chosen background and text color.

The Code

File I:bgchooser.html

<html>
<head>
<title>indiWiz.com PHP Tutorial</title>
</head>
<body>
<p>Choose Colors for the next page:
<ul>
<li><a href="bgprocess.php?bg=red&text=black">Red Background with Black Text</a>
<li><a href="bgprocess.php?bg=green&text=red">Green Background with Red Text</a>
<li><a href="bgprocess.php?bg=blue&text=white">Blue Background with White Text</a>
</ul>
</body>
</html>

Note: All the links call the file bgprocess.php. But as you notice, after the filename bgprocess.php there is a question mark(?). The part after the question mark in the URL indicates the script variable values. The variables are bg and text. Both are separated by an ampersand(&) sign.

File II:bgprocess.php

<html>
<head>
<title>indiWiz.com - PHP Tutorial</title>
</head>
<body bgcolor="<?php echo $_GET['bg']; ?>" text="<?php echo $_GET['text']; ?>">
<p>Customised Background & Text!
</body>
</html>

Note: In the <body> bgcolor attribute, we print the value contained in the variable $_GET['bg']. Similarly, for the text attribute of the <body> tag, we print the value contained in the variable $_GET['text'].

Demo of this example.


User Comments

Add Comment

[Quick Stats: Number of main threads: 4, Number of sub-threads: 3]


Sign Guestbook | Who is Subhash?
The contents of this site are copyright© 2000-2008, indiWiz.com. All Rights Reserved.