.
Developer Spot - Web Development Tutorials
arrowDeverloper Spot  Tutorials  PHP  Using HTML Forms With PHP 
 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Web Development News
PHP Manual
Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Using HTML Forms With PHP

By Nicholas Chase
2004-01-09
Reader Rating: 5 out of 5
Bookmark Print Version
The Old Way: Accessing Global Variables

The code shown in Listing 2 treats the forms values as globals:
Listing 2. Form values as globals



<?php
echo "Ship = ".$ship;
echo "<br />";
echo "Tripdate = ".

$tripdate;
echo "<br />";
echo "Exploration = ".$exploration;
echo "<br />";
echo "Contact = ".$contact;
?>


The resulting Web page shows the submitted values:



Ship = Midnight Runner
Tripdate = 12-15-2433
Exploration = yes
Contact =


(As you'll see in a minute, there's no value for Contact because that box wasn't checked.)

The notation in Listing 2 is certainly convenient, but it is only available if the PHP directive register_globals is set to on. Prior to version 4.2, this was the default, and many PHP developers may even be unaware that there's an issue. Starting with version 4.2, however, the default setting for register_globals is off, in which case this notation doesn't work because the variables are never created and initialized with the appropriate values.

You can, however, use other ways to initialize these variables. The first is to change the value of register_globals. Many developers who work on shared servers don't have the option to change this value for the entire server, but behavior can be changed for a particular site. If you have access to the .htaccess file, you can turn on register_globals by adding the directive:


php_flag register_globals on

Because of the uncertainty in whether this feature is available, developers are advised that it's better not to use or rely on this method of acquiring variables. So what options do you have? If your system is running version 4.1 or higher, another option is to selectively register sets of globals using import_request_variables(). You can use this function to import get, post, and cookie values, and to add a prefix to each, if desired. For example:


<?php
import_request_variables(gp, "formval_");
echo "Ship = ".$formval_ship;
echo "<br />";
echo "Tripdate = ".$formval_tripdate;
echo "<br />"

;
echo "Exploration = ".$formval_exploration;
echo "<br />";
echo "Contact = ".

$formval_contact;

?>


Here, both get and post values are imported -- use c to import cookie values -- and because p follows g, post values will override get values of the same name.

But what if, like many developers, you're not running version 4.1 or higher?


Article Pages:
Access Single and Multiple Form Values
The HTML Form
The Old Way: Accessing Global Variables
Accessing Form Value Collections
One Name, Multiple Values
The Amazing Disappearing Checkboxes
Getting All Form Values
One Final Note: Dots
Summary
Resources

First published by IBM developerWorks


 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 4

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics



 
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML
More Resources: Web Hosting Articles - Web Development News - PHP Manual