Page 274 - Beginning PHP 5.3
P. 274

Part III: Using PHP in Practice
                  However, it ’ s generally a good idea to write your code so that it doesn ’ t generate notices. This helps to
                 ensure the robustness and security of your application. This means that you should check for the
                 presence of a submitted form field before using it, rather than assuming that it exists. You can do this
                 using PHP functions such as   isset()  or  array_key_exists() :

                          <dt>Gender</dt><dd><?php if ( isset( $_POST["gender"] ) ) echo $_
                    POST["gender"]?></dd>




                  Dealing with Multi - Value Fields

                   You learned earlier in the chapter that you can create form fields that send multiple values, rather than a
                 single value. For example, 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
                    ‘Widget Times’ 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.

                  So how can you handle multi - value fields in your PHP scripts? The trick is to 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

              236



                                                                                                      9/21/09   7:23:38 PM
          c09.indd   236                                                                              9/21/09   7:23:38 PM
          c09.indd   236
   269   270   271   272   273   274   275   276   277   278   279