Welcome to the World of PHP Page 7

PHP Variables and Web Forms

Note that addToMaillist is a fictional function, that we might have created to place the user's e-mail address into a text file somewhere.

Because PHP migrates form field names into variables, you may wish to construct form field names with an eye toward their resulting data structure in PHP. For example, suppose you have one form that asks for information about a recipient of some sort. You may want to contain that form's values inside a $recipient array, for example, to improve data management inside your PHP script:

<FORM action="process.php3" method="get"> Please enter the recipient's e-mail address: <INPUT type="text" size=20 name="recipient[email]"><BR>
Please enter the recipient's full name: <INPUT type="text" size=20 name="recipient[name]"><BR>
<BR><INPUT type="submit"> </FORM>


<?php while (list($key,$value) = each($recipient)) { print "$key: $value<BR>"; } ?>

The HTML form above was migrated into a PHP array named $recipient with the keys email and name. Above, our PHP code simply reproduces the loop we saw earlier and outputs each key and value in this array, for example (depending on what was submitted in the form):

email: cat@doglover.net name: Mrs. Kitty


This article was originally published on Jul 7, 2000

Thanks for your registration, follow us on our social networks to keep up-to-date