RU beehive logo ITEC dept promo banner
ITEC 325
2013spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect08b-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' selected='selected' /></label>
      <select>
        <option selected='selected'>Option 1</option>
        <option>Option 1</option>
      </select>
    

    (b) But to do this, the page that *makes* the form has to be the
         same as the page that *receives* the form!
    (c) Imagine a big if-statement:
             if (this-form-was-submitted) {
                 ...validate and print server-side error message...
                 }
           
             if (form-was-submitted && data-is-valid) {
                 ...handle the accepted form stuff...
                 }
             else {
                 ...print the form...
                 echo "<input type='text' name="lastName" value=??? maxlength=??? />
             }
     (c2) example: lect08b-sticky-form.php
             
     (d) <input type="hidden" name="i-came-from-the-form" value="true" />
     (e) You can include files:
                require         include       require_once      include_once

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

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2013.Mar.20 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme