PHP - Dealing with Multi-Value Fields

Introduction

The following form fields are capable of sending multiple values to the server:


<label for="favoriteWidgets" >What are your favorite widgets?</label>
<select name="favoriteWidgets" id="favoriteWidgets" size="3" multiple="multiple" >
    <option value="superWidget" >The SuperWidget</option>
    <option value="megaWidget" >The MegaWidget</option>
    <option value="wonderWidget" >The WonderWidget</option>
</select>

<label for="newsletterWidgetTimes" >Do you want to receive our 'PhoneTimes'newsletter?</label>
<input type="checkbox" name="newsletter"  id="newsletterWidgetTimes " value="widgetTimes" />
<label for="newsletterFunWithWidgets" >Do you want to receive our 'Fun with Widgets'newsletter?</label>
<input type="checkbox" name="newsletter" id="newsletterFunWithWidgets" value="funWithWidgets" />

The first form field is a multi-select list box, allowing the user to pick one or more (or no) options.

The second two form fields are checkboxes with the same name (newsletter) but different values (widgetTimes and funWithWidgets).

If the user checks both checkboxes then both values, widgetTimes and funWithWidgets, are sent to the server under the newsletter field name.

To handle multi-value fields in your PHP scripts, add square brackets ([]) after the field name in your HTML form.

Then, when the PHP engine sees a submitted form field name with square brackets at the end, it creates a nested array of values within the $_GET or $_POST (and $_REQUEST) superglobal array, rather than a single value.

You can then pull the individual values out of that nested array.

So you might create a multi-select list control as follows:

<select name="favoriteWidgets[]"  id="favoriteWidgets"  size="3"  multiple="multiple" ...  < /select >

You'd then retrieve the array containing the submitted field values as follows:

$favoriteWidgetValuesArray = $_GET[" favoriteWidgets" ];  // If using get method
$favoriteWidgetValuesArray = $_POST[" favoriteWidgets" ]; // If using post method

Example

The form handler deals with these multi-value fields, displaying their values within the Web page.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>Membership Form</title>
</head>
<body>
Membership Form
<p>Thanks for choosing to join The PhoneClub. To register, please fill in your details below and click Send Details.</p>
           <form action="process_registration_multi.php" method="post" >
             <div style="width: 30em;" >
               <label for="firstName" >First name</label>
               <input type="text" name="firstName" id="firstName" value="" />

               <label for="lastName" >Last name</label>
               <input type="text" name="lastName" id="lastName" value="" />

               <label for="password1" >Choose a password</label>
               <input type="password" name="password1" id="password1" value="" />
               <label for="password2" >Retype password</label>
               <input type="password" name="password2" id="password2" value="" />

               <label for="genderMale" >Are you male...</label>
               <input type="radio" name="gender" id="genderMale" value="M" />
               <label for="genderFemale" >...or female?</label>
               <input type="radio" name="gender" id="genderFemale" value="F" />

               <label for="favoriteWidgets" >What are your favorite widgets?</label>
               <select name="favoriteWidgets[]" id="favoriteWidgets" size="3" multiple="multiple" >
                 <option value="superWidget" >The SuperWidget</option>
                 <option value="megaWidget" >The MegaWidget</option>
                 <option value="wonderWidget" >The WonderWidget</option>
               </select>

               <label for="newsletterWidgetTimes" >Do you want to receive our 'PhoneTimes' newsletter?</label>
               <input type="checkbox" name="newsletter[]" id="newsletterPhoneTimes" value="widgetTimes" />
               <label for="newsletterFunWithWidgets" >Do you want to receive our 'Fun with Widgets'newsletter?</label>
               <input type="checkbox" name="newsletter[]" id="newsletterFunWith Widgets" value="funWithWidgets" />
               <label for="comments" >Any comments?</label>
               <textarea name="comments" id="comments" rows="4" cols="50" ></textarea>

               <div style="clear: both;" >
                 <input type="submit" name="submitButton" id="submitButton" value="Send Details" />
                 <input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;" />
               </div>
             </div>
           </form>

         </body>
       </html>

Now save the following script as process_registration_multi.php in your document root folder:

       <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
         <head>
           <title>Thank You</title>
         </head>
         <body>
           Thank You
           <p>Thank you for registering. Here is the information you submitted:</p>
       <?php

       $favoriteWidgets ="" ;
       $newsletters ="" ;

       if (isset($_POST[" favoriteWidgets" ])) {
         foreach ($_POST[" favoriteWidgets" ] as $widget) {
           $favoriteWidgets.= $widget." ," ;
         }
       }

       if (isset($_POST[" newsletter" ])) {
         foreach ($_POST[" newsletter" ] as $newsletter) {
           $newsletters.= $newsletter." ," ;
         }
       }
     $favoriteWidgets = preg_replace(" /, $/" ," " , $favoriteWidgets);
     $newsletters = preg_replace(" /, $/" ," " , $newsletters);
?>
         <dl>
           <dt>First name</dt><dd><?php echo $_POST[" firstName" ]?></dd>
           <dt>Last name</dt><dd><?php echo $_POST[" lastName" ]?></dd>
           <dt>Password</dt><dd><?php echo $_POST[" password1" ]?></dd>
           <dt>Retyped password</dt><dd><?php echo $_POST[" password2" ]?></dd>
           <dt>Gender</dt><dd><?php echo $_POST[" gender" ]?></dd>
           <dt>Favorite widgets</dt><dd><?php echo $favoriteWidgets?></dd>
           <dt>You want to receive the following newsletters:</dt><dd>
           <?php echo $newsletters?></dd>
           <dt>Comments</dt><dd><?php echo $_POST[" comments" ]?></dd>
         </dl>

       </body>
     </html>

Related Topic