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

homelectshws
D2Lbreeze (snow day)

lect26-sticky
sticky forms

Sticky forms

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 value for text fields, and selected/checked for other elements:

      <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: lect26-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=??? />
             }
(c1) How can we tell if this form was submitted vs a first-time visit?
(c2) for the last two branches -- can we keep our file/function from getting too long?

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: require other files (existing from previous hws!).

(d) The result:

             // A new top-level form, "the-real-form.php"

             if (array_key_exists('form-was-submitted', $_POST)
                 $errMsgs = validateAll(); //collect all error messages
                 }
             else {
                 $errMsgs = array();  // No errors -- first-time-visit.
                 }
                 

             if (array_key_exists('form-was-submitted', $_POST) && !$allErrorMsgs) {
                 require('form-handle.php');  // Successful submit!
                 }
             else {
                 require('form.php');
                 // This file is our previous form updated so as to:
                 // - print all contents of $errMsgs,
                 // - be sticky
                 // - submit to "the-real-form.php"
                 // - not include the opening and closing html
                 }
    
Note that as a sticky-form, we are submitting to ourselves (although that's kinda obscured by the requires). I named this new one "the-real-form.php", which required either form-handle.php or form.php, but that's partly to preserve names with our previous arrangement. In practice, I'd probably name these files along the lines of form.php, form-handle.php, and form-print.php.


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..      

homelectshws
D2Lbreeze (snow day)


©2014, Ian Barland, Radford University
Last modified 2014.Nov.13 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.