RU beehive logo ITEC dept promo banner
ITEC 325
2018fall
ibarland

grouping web-inputs into arrays

grouping inputs into a single array

What if we have several different inputs which are grouped together? For example, in addition to asking about favorite colors and quests, you have several inputs all about a mailing-address (street, city, state, and zip). We could have the handler receive each input separately:

array( "favorite-color" => "orange"
     , "quest" => "cookies"
     , "street" => "800 E. Main St"        // }
     , "city" => "Radford"                 // } unsatisfying to not have
     , "state" => "VA"                     // } these four grouped more closely
     , "zip" => "24121"                    // }
     , "is-happy" => "yep"
     );
          
But clearly, this is unsatisfying: the four inputs for street, city, state, and zip are closely related in a way that the other inputs aren't. We know from programming, that we'd like to have all the address-information grouped into its own object/array:
array( "favorite-color" => "orange"
     , "quest" => "cookies"
     , "address" => array( "street" => "800 E. Main St"        // }
                            , "city" => "Radford"              // } Much better! The address
                            , "state" => "VA"                  // } is now a single object!
                            , "zip" => "24121"                 // }
                            )
     , "is-happy" => "yep"
     );
          
In intro programming, this is the same notion of an object, one of whose fields happens to be another object. (An array containing an array is the natural, proper representation.)

Happily, we can achieve this! If an input tag's name attribute contains square-brackets, a (sub) array is created inside $_POST, exactly like we want:

 <input type='text'     name='favorite-color'/>
 <input type='text'     name='quest'/>
 <input type='text'     name='address[street]'/>
 <input type='text'     name='address[city]'/>
 <input type='text'     name='address[state]'/>
 <input type='text'     name='address[zip]'/>
 <input type='checkbox' name='is-happy' value='yep'/>
            
In our form-handler, $_POST['address'] is now an entire sub-array. So we can pull out elements of that array by following it with square-brackets: $_POST['address']['zip']. Some people also prefer to pull out the sub-array and put it into its own variable, though that's not strictly necessary:
              echo "favorite color is ", $_POST['favorite-color'], "\n";
              echo "the address's zip is ", $_POST['address']['zip'], "\n";
              // Or, we can make a new variable to hold just the sub-array:
              $address = $_POST['address'];
              echo "the address's city is ", $address['city'], "\n";
            
pro tip: If you do want to grab a value from an array and stick it in a variable (rather than just typing out the array-and-index every time you want to use it), do yourself a favor and make the variable-name the same as the index-name:
                $faveCol  = $_POST['favorite-color];  // ACK this way lies madness
                $is_happy = $_POST['is-happy'];       // Good — parallel naming reduces mental overhead
              

The only thing to note is a slight discrepency in quote-marks, in the html source (no quotes inside the square-brackets), and the PHP code accessing an array-index (where you do put quotes inside the square-brackets1).

video from distance lecture (breeze), 2017-feb-07 (1h42m), REVIEWING this info

1 Well, admittedly: PHP, in its awfulness, will interpret text-without-quotes as a string, so it would actually work to look up an array-index without putting quote-marks inside the square brackets. But that's CRAZY. Put quotes around string-constants, like we do in every other language on the planet (at least, if you want full points, in this course).      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.