|
home—lects—hws
D2L—breeze (snow day)
Forms and form-handlers:
A form is an html page where users may enter information
(e.g. their favorite color, and whether or not they feel happy).
Then, “submitting the form” just means requesting some other URL,
but including the users's information along with the request.
Slightly more detail:
When a user clicks submit,
the browser takes the URL specified in the
The main thing to note is how the key-value pairs are specified.
the form has
Of secondary interest is the
We've already discussed that when
<input type='text' name='txt1' size='10' maxlength='8' id='txtone' /> |
<input type='number' name='age' size='5' maxlength='3' min='21' max='30' value='25' id='age' /> |
<input type='checkbox' name='cb1' value='milk' checked='checked' /> |
<input type='radio' name='age' value='over50' id='age-over50' /> <br/> <input type='radio' name='age' value='30to50' id='age-30to50' /> <br/> <input type='radio' name='age' value='under30' id='age-under30' /> <br/> |
exceedingly optional: How the original radio-buttons worked: What happens in an old radio, when you turn the dial — how does a tuner actually receive different frequencies?Well, in order to pick up a different frequency, you want an antenna of a different length. That's because when a radio wave pushes the electrons in a wire, it's like pushing somebody on a swingset: if you push somebody at the right interval they'll go higher; you're pushing them at the right "frequency" for that swing-length. But if you push them at the wrong interval (say, at 30% of their swing, then 60%, then 90%, then 120% = 20% of their way back down), they go nowhere (your work is interfering with itself, yielding little-to-no net effect). In a piece of wire, all the ambient radiowaves are simultaneously pushing electrons at different timings, but because the electrons bounce/reflect off the end of the wire, only one frequency gets useful results (and that frequency depends on the length of the wire).
Okay, so we want tuning a radio dial to make the antenna longer or shorter. Hmm, that's hard to implement, physically. However, there is a clever way to change its effective length: we can change the wire's capacitance instead (how hard it is to push the electrons along the wire). A variable-capacitor can be made by having two sets of interleaving metal plates; the amount the plates overlap changes the capacitance. video snippet (13sec): A variable-capacitance tuner
So: in old-timey radios, the tuning dial is connected (just behind the faceplate) to a belt which turns the wheel we see the man rotating-by-hand in the video. This changes the amount of overlap of the interleaved-plates, which changes the capacitance, which changes what frequency will successfully push the electrons in the antenna!
That just leaves the radio-buttons, to explain: the buttons were hooked up to a gear which had a belt to the tuning-dial. When you pressed a button, the tuning-dial also got spun! (As you might guess, you had to press the button pretty hard, to cause the tuning-dial to spin!)
…Nowadays, the buttons and dial are all electronic, and soon cars will be not just self-driving, but they will detect your mood, look up your favorite Pandora stations, and start streaming songs (and ads) without you doing a thing.
<textarea name='comments' id='comments' rows='3' cols='30'> Initial Contents </textarea> |
(I'm not really sure why text areas are different from
from checkboxes/radio-buttons/text-fields2.
It'd seem more sensible to either have a single tag
<select name='title' id='title'> <option value = 'mr'>Mr.</option> <option value = 'ms'>Ms.</option> <option value = 'mrs'>Mrs.</option> <option value = 'miss'>Miss</option> </select> |
<input type='submit' value='Submit Data to Server' /> |
<input type='reset' value='Clear Form' /> |
You can use a label tag, to connect some text (anywhere on page) with (sending focus to) a particular input: See the example form below. Either enclose the text and the input inside the same label, or use label's for attribute.
Gotcha: While <input name='aNodeName'/> uses the name attribute to pass information to the action/request, the html <label for='aNodeId'>… uses id to refer to the DOM node of the current tree. (This actually makes good sense, but takes a moment of thought.)
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-brackets3).
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.Mar.18 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |