home—lects—hws
D2L—breeze (snow day)
hw03
hw03: form-handling
and html sanitizing
due Oct.03 (Mon), 11:00
You have been hired to help develop the game Okaymon -- a crowdsourced
game of evolving monsters!
You need to develop a web form with the information below;
when the form is submitted, the user is shown a page which simply
re-caps the submitted information.
In this homework, we will
(A) add more utility helper-functions for producing html to utils.php,
and then
(B) write a form with a
simple form-handler which just
displays the information received by the form.
Part A
Write the following functions in php:
- (10pts) dropdown : array, string → string, which produces the html for a
drop-down menu.
(To think about: the array is what to have as the options for the drop-down.
What is the string for?
This will be clearer after you make a test case for a usable drop-down.)
Note that half the points are for having good tests, on this problem and others.
Include an additional, optional input,
of type boolean-or-string:
A true indicates to include “select one” as the
first entry in the drop-down;
it wouldn't correspond to a meaningful choice.
Providing a string is similar, except that
instead of the words “select one”,
use the provided string.
Finally, false indicates to not include any such item.
The default value should be true, if it's not explicitly passed in.
(10pts) Write radioTableRow : string, array → string
which produces a bank of radio-buttons
inside of a table-row.
For example, radioTableRow( "clover", array("weak-to","neutral","resistant") )
would return the first row of the table in the screenshot below.
Note that it does not return a table tag,
just a tr tag.
As you create your test cases, you'll need to
think about and decide
how to generate each radio-button's name and value.
- (2pts)
As we wrote together in lecture,
write radioTable : array, array → string
which returns the html for a table of radio-buttons
(including the column-headers, taken from the first array).
This function will naturally call radioTableRow.
You must have test-cases for it.
Part B
Make a form (10pts) and a form-handler (10pts) for entering new species of Okaymon,
including tests — see “handle-sample” below.
Your forms don't need to look exactly like the above,
but should have the same information
(and look respectable).
-
The units for weight should
be kg and lbs.
-
You should have the five energy-type resistances listed in their own section.
-
No data-validation is required, but
when you print the results you must sanitize the html:
the information must render as the user entered it.
(Hint: make a helper function, and test it.)
-
In order to test your form, you must also have three files
“okaymon-handle-sample-N.php” (for N = 0,1,2)
which populate $_POST with (respectively): (0) no information, (1) typical inputs, and (2) input
which includes characters like
“>”
or
“'”
or
“\n”.
-
For the copyright notice,
use a label tag, so that
the user can click on the text to as well as the checkbox itself.
-
Use an array to hold the various energy-types:
if we add twenty more energy-types, you shouldn't need to change anything but that array,
and everything else should work.
Of course, you should call functions from part (a) appropriately,
and write any additional functions (and tests) as appropriate.
Deliverable:
Your form must be viewable on-line at
https://php.radford.edu/~yourUserID/itec325/hw03/okaymon-form.php.
Submit hardcopy of
utils.php, utils-test.php,
the form, the form-handler (named “okaymon-handle.php”),
and at least three “okaymon-handle-sample-N.php” files.
On D2L submit all your files (as individual files, not a .tar).
Bring hardcopy to the class following the deadline.
home—lects—hws
D2L—breeze (snow day)