|
home—lects—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> <label>ageless: <input type='radio' name='age' value='under20'/></label> <select name='mealplan'> <option>Vegetarian</option> <option selected='selected'>Carnivore</option> </select> |
What php is used, to generate/print the above HTML?
<label>age: <input type='radio' name='age' value='over50' <?php if $_POST['age']=='over50' { echo "checked='checked'"; }?> /></label> <label>ageless: <input type='radio' name='age' value='under20' <?php if $_POST['age']=='under20' { echo "checked='checked'"; }?> /></label> |
Note that if you already have a php-function for creating drop-downs, or a bank of radio-buttons, etc.
then
you can upgrade it to take in one more argument — the option you want pre-selected.
(or, just
But wait a minute:
How do we use
Explanation: We originally had two files:
// this is info-form.php (version 0 of 2), a wrapper for info-form-helper.php and info-handle.php // $errMsgs = validateAll(); // A php function which checks all other input forms, similar to our validateAll in .js soln; // returns an array-of-error-messages if (!$errMsgs) { require 'info-handle.php'; // Form successfully validated } else { print_r($errMsgs); // Well, let's print them nicer-looking than that. But you get the point. require 'info-form-helper.php'; // Just like before, except sticky, and submits to 'real-form.php' } |
This is pretty good. However, there is still one big conceptual glitch (and, some smaller practical ones):
If you fill out the form and submit to this new info-form.php, things are great:
// this is info-form.php (version 1 of 2), a wrapper for info-form-helper.php and info-handle.php // $errMsgs = validateAll(); // A function which checks all other input; returns array of error-messages. if (!$errMsgs) { require 'info-handle.php'; } else { if (got-here-via-submitting-form), then { print_r($errMsgs); // Well, let's print them nicer-looking than that. But you get the point. } require 'info-form-helper.php'; // Just like our previous info-form.php, except sticky, and submits to this new 'info-form.php' } |
How can we magically know if we
We'll fix this discrepancy by
ensuring that
There are still some practicalities to handle:
<html> <head>…<head> <body> <?php // this is info-form.php (version 2 of 2), a wrapper for info-form-helper.php and info-handle.php // if ($_POST) { $errMsgs = validateAll(); // A function which checks all other input; returns array of error-messages. } if (!$errMsgs) { require 'info-handle.php'; } else { echo "<h3>Form title</h3"; if ($_POST) then { echo "Errors exist:"; print_r($errMsgs); // Well, let's print them nicer-looking than that. But you get the point. } require 'info-form-helper.php'; // Just like old info-form.php, except sticky, and submits to this new 'info-form.php' } ?> |
A note on testing:
How can you test your page, for various inputs? It's a pain to keep pulling up the form and submitting.
You can automate much of this by:
// This is the file info-form-test-1.php $_POST = array( 'got-here-via-submitting-form' => 'a-yep', 'firstName' => 'Amy', 'creditCardNum' => '1234-5678-8765-4321' ); require('info-form.php'); |
Note that a “pure” way to go would be to use functions, instead of including files:
a function
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
Okay, text-input-boxes might be in the array mapped to the empty-string;
however if our form had nothing but checkboxes, then
home—lects—hws
D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Apr.04 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |