|
home—lects—exams—hws
D2L—breeze (snow day)
A “sticky” form is one where, when you re-visit the form, your previous information is already filled in. In particular, we'll think of the scenario where somebody submits a form to the server, but the server rejects it — perhaps due to server-side validation, but it could also be because of session-timed-out, or item-out-of-stock1 or offer-has-expired, or whatever.
The html for an input form with a pre-filled value is easy:
You just include the attribute
<label>First name: <input type='text' name='fname' value='Herman' /></label> <label>age: <input type='radio' name='age' value='over50' checked='checked'/></label> <select> <option>Option 1</option> <option selected='selected'>Option 2</option> </select> |
But to do all this, the page that makes the form has to be the same as the page that receives the form! Example: lect27-sticky-form.php
BUT: if we submit to ourselves, we need to distinguish between two cases:
(c) Imagine a big if-statement:
if (the form was submitted) collect all error messages } else { (no error messages) } if (form-was-submitted && !$allErrorMsgs) { ...handle the accepted form stuff... } else { ...print error messages, if any.... ...print the form, stickly: echo "<input type='text' name="lastName" value=??? /> } |
The answer for c1: Add a name/value to the submit button itself:
<input type="submit" name='form-was-submitted' value='blah-blah-this-value-does-not-matter'/>
The answer for c2:
(d) The result:
// A new top-level file, "form-wrapper.php" if ( |
Looking forward:
Sometimes a sticky-form's info comes from an initial attempt to fill it in.
But sometimes you get a partially-filled form because the existing-info comes from a database
instead of
1 Of course, hopefully when an item is out of stock, the server might check that before having customers fill out the order form — perhaps before even showing the product page at all. It's more server load to do this, but much better user-friendliness. You can find a compromise by having a "check availablity" button, or checking availability when they put it into their cart, etc.. ↩
2
It's poor practice to use globals like this;
we'd rather pass parameters.
So our
home—lects—exams—hws
D2L—breeze (snow day)
©2015, Ian Barland, Radford University Last modified 2015.Mar.27 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |