[UPHPU] variable declaration for multiple form fields -help

David Smith davidsmith at byu.net
Fri May 14 20:47:44 MDT 2004


jeffrey neslen wrote:

>Sorry this question might be pretty basic, but forgive
>me since I am just a newby.
>
>I have a form that has several fields (hundreds) and
>each field has a unique name and even a variety of
>input types including check boxes.
>When I post these items to my php script I need to set
>a unique variable for each form item by placing them
>in a local variable or setting them to a default value
>if the field is not passed like so...
>
>if(isset($_POST['my_feild'])){
>   $my_feild_var = $_POST['my_feild'];
>} else {
>   $my_feild_var = "";
>}
>
>now this would be very easy if I had one or two
>fields, but I have hundreds and need to come up with a
>solution.  I know I could use a foreach loop to
>generate an array[], but this would not produce the
>unique names I need for variable declarations/setting.
>Any ideas/solutions or alternatives to my conundrum?
>

One technique that I commonly employ for handling large (and possibly 
dynamic) sets of form inputs is PHP style array naming on the HTML form 
side. Just name your inputs like so:

<input type="text" name="input[0]" /><br>
<select name="input[1]">
    <option>Option 1</option>
    <option>Option 2</option>
</select>
<input type="password" name="input[2]" /><br>

Then, PHP will automatically take all these input values and store them 
in a single array:

if( isset( $_POST['input'] ) && is_array( $_POST['input'] ) )
    foreach( $_POST['input'] as $i => $value ) {
       // handle the input
   }

Now, you can even use intuitve naming in your HTML inputs to create an 
associative array.
<input type="text" name="input[first_name]" /><br>
<input type="text" name="input[last_name]" /><br>
<select name="input[gender]">
  <option>male</option>
  <option>female</option>
</select>

And the resulting $_POST['input'] will be an associative array with 
indexes 'first_name', 'last_name', and 'gender'. I find this very 
useful, though it doesn't play well with JavaScript. I have a 
work-around to make JavaScript play nicely with form names like this. 
Inquire if interested.

The benefit of this technique is that you can name your form fields to 
match table column names for your database, or at least use a 
translation and then just grab the inputs and store them. The PHP code 
won't need to change much  if (when) HTML inputs are added to or removed 
from the form. It is also not as error prone as fetching and validating 
the $_POST['...'] index for each expected input.

Lastly, as a possibly superior option, I recommend you investigate HTML 
form libraries likes RazorForm[1] as it likely provides this kind of 
functionality and will be much more robust than any home grown solution. 
It can also be used to generate your HTML form and do auto-validation of 
all your fields based on a dynamic set of criteria you specify.

Good luck!

--Dave

[1] http://www.twoedge.com/programming/php/RazorForm/index.php




More information about the UPHPU mailing list