.
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
One Name, Multiple Values

So far, each name has corresponded to only one value. What happens when there's more than one value? The crew species list box, for example, allows multiple values to be submitted with the name crew.

Ideally, you'd like these values to be available as an array so you can retrieve them explicitly. To make that happen, you've got to make a slight change to the HTML page. Fields that are to be submitted as arrays should be named with brackets, as in crew[]:

Listing 4. Modifying the HTML page


...
<td>
<select name="crew[]"

multiple="multiple">
<option value="xebrax">Xebrax</option>

<option value="snertal">Snertal</option>
<option value="gosny">Gosny</option>#
</select>
</td>
...


Once you've made this change, retrieving the form value actually yields an array:

Listing 5. Accessing the variables as an array


...
$crew_values = $HTTP_GET_VARS

['crew'];
echo "0) ".$crew_values[0];
echo "<br />";
echo "1) ".$crew_values[1];
echo "<br />";
echo "2) ".$crew_values[2];

...


Submitting the page now shows the multiple values:


0) snertal
1) gosny
2)

Notice first that this is a zero-based array. The first value encountered is in position 0, the next in position 1, and so on. In this case, I submitted only two values, so the third spot is blank.

Normally, you don't know how many items will be submitted, so instead of calling each item directly you can use the fact that it's an array to your advantage, using the sizeof() function to determine how many values were submitted:

Listing 6. Determining the size of the array
...

for ($i = 0; $i < sizeof($crew_values); $i++) {

echo $crew_values[$i];
echo "<br />";
}
...


Sometimes, however, the problem is not too many values, but no values at all.


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