|
home—lects—hws
D2L—breeze (snow day)
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" ); |
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" ); |
Happily, we can achieve this!
If an input tag's
… <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'/> |
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).
Q: But how is information communicated from a web form (pure html) to a php program?
A: When you click 'submit', the HTML makes a page-request to the page specified by the form's "action" attribute.
(Presumably it's a php page.)
The page-request incudes extra information about what html input fields had been selected, etc.,
as part of the http request.
The server gets that request and invokes the php file as normal, but it also
pre-initializes an array of values for the program — and it fills that array with
the extra information contained in the page-request.
Best practice:
when including a
It's not so much that you always need both name and id, but it's not uncommon, and you should really name them the same thing. (This page made them slightly different, just so you could see the difference, and understand which is used where).
home—lects—hws
D2L—breeze (snow day)
©2017, Ian Barland, Radford University Last modified 2017.Sep.25 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |