.
Developer Spot - Web Development Tutorials
 


Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Choosing The Right Server-Side Scripting Language

By Craig McElwee
2004-01-06
Reader Rating: 4 out of 5
Bookmark Print Version
Task 2: Put Form Field Data Into Variables

Each language has its own methodology and idioms for getting the "name=value" pairs from the environment. For the most part, these basic tricks of the trade will simply be cut and pasted into each new script, and modified as necessary, which is to say, not much. When a Web server calls an external program, the data sent by the client's browser are assigned to specific variables in that program's environment. For an HTML form that used the post method, the program has to read the number of bytes in the environment's "Content Length" variable from standard in (STDIN). The program variable that holds the data from STDIN now holds a string of "name=value" pairs, and will need to separate these into chunks, URL-decode the values, and then put them into variables for processing.

Here are some examples (note there is no error checking):

Python uses a module that simplifies the task considerably, allowing almost direct access to the values for assignment into variables, here, data and data2:



form = cgi.FieldStorage()
data = form['data'].value
data2 = form['data2'].value


PHP and Java servlets let you skip the first step and jump right to assignment, as long as you get the variable names you expect. For more generic processing, you would iterate through the list of received variables and assign them dynamically.



$data = $HTTP_POST_VARS["data"];
$data2 = $HTTP_POST_VARS["data2"];


Java:
String data = request.getParameter("data");

String data2 = request.getParameter("data2");

Perl's standard block of code isn't hidden away in a module. Here, all the "name=value" pairs are taken out of the string and each assigned to a slot in the @pairs array.

Perl:


read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

Then we iterate through the array, putting the name and value into variables:



foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);


Since the values submitted via the browser have been "URL encoded" for transmission to the server, we now must "URL-decode" the values, first by changing all the +s to spaces, then converting all the escape codes back to their original values.

    


$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


Finally we assign whatever is on the right side of the = (equal) sign as the value of a variable named whatever is on the left side of the = sign.



$Form

{$name} = $value;
}



The Tcl code follows this same methodology. Having to figure out this logic for every program would be tedious and error prone. Fortunately you can just cut and paste those blocks verbatim in any CGI program.



Article Pages:
How Five Languages Do The Same Basic Tasks
Task 1: Get and Format The Time/Date
Task 2: Put Form Field Data Into Variables
Task 3: Search and Replace
Task 4: File Writing
Task 5: File Reading
Task 6: Split Comma-Delimited Line Into Variables
So, Which One Should You Use?
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
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




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