|
home—lects—hws
D2L—breeze (snow day)
We've been talking about input forms (html elements), and then php scripts to process a submitted form:
<!-- inside form.php (or, form.html) --> <form submit='submit.php' action="post"> Login: <input type='text' name='userid' /> <br /> Remember me?: <input type='checkbox' name='remember' value='yep' /> … </form> |
<!-- inside submit.php --> <?php echo "Hey there ", $_POST['userid']; if (array_has_index('remember',$_POST)) { echo "How could I ever forget?"; // $_POST['remember'] = 'yep' } ?> |
When creating a page which includes something typed in by the user (from a previous text-input), we must be careful! What could the user have typed, that would goof up our web page?
Note that to prevent errors, some php servers used to
enable “magic quotes”:
When setting up the
A poor solution is to always call
/** Return the input from a user form. * @param The field-attribute-name to look up, from the original form. * @return the input from a user form, */ function getInput($name) { $formInput = |
Quick q: suppose a user types:
hi <3in a textarea whose name is
We have seen arrays,
and mentioned that if they have all-numeric indices (keys) then
we can process them with a
Then we saw that if an array has keys which aren't all numeric,
we can use a
$myData = array( 'hi' => 'hallo', 'good day' => 'guten Tag', 'see you later' => 'auf wiedersehen' ); foreach ($mydata as $german) { echo $german, "\n"; } foreach ($mydata as $english => $german) { echo "You say '$english', I say '$german'.\n"; } |
Look at the documentation for
Since it returns an array of filenames,
it's a natural match to use with other functions that want an array of strings:
For example,
Suppose we wanted an English list of hyperlinks, separated by commas, with the word "and" before the last item. This decomposes into two orthogonal parts:
To create the array, hopefully you also used your function
$URLsAsText = array( "http://d20srd.org", "http://www.radford.edu", "http://google.com" ); $URLsAsHTML = array(); foreach ($URLsAsText AS $url) { $URLsAsHTML[] = hyperlink($url); } echo "It should appeal to users of ", commaSeparatedList( $URLsAsHTML ), "." |
Any other repeated stuff?
Hmm, the “http:” prefix
was kinda annoying, but writing a loop for that seems definite overkill.
(Design Question: Should
It's kinda annoying to keep writing loops that
make a new array of updated values.
Most of the loop is very rote — the only part that differs is
the particular rule to transform the individual element to the new element.
(In the example above, the answer is
“the function
There is a handy function,
$URLsAsText = array( "http://d20srd.org", "http://www.radford.edu", "http://google.com" ); $URLsAsHTML = array_map( $URLsAsText, "hyperlink" ); echo "It should appeal to users of ", commaSeparatedList( $URLsAsHTML ), "."; |
$URLsAsText = array( "http://d20srd.org", "http://www.radford.edu", "http://google.com" ); echo "It should appeal to users of ", commaSeparatedList( array_map( $URLsAsText, "hyperlink" ) ), "."; |
Finally, note that we can also handle the
“prepend “http://” to each item” issue.
We could make a separate function and apss that to
$URLsAsText = array_map( array( "d20srd.org", "www.radford.edu", "google.com" ), function ($domain) { return "http://" . $domain; } ); |
1 Heck, if you don't even want to name the original array, you could inline that. This is arguably in-lining too much, but that can be an issue of taste (and requires taking care with indentation):
echo "It should appeal to users of ", commaSeparatedList( array_map( array( "http://d20srd.org", "http://www.radford.edu", "http://google.com" ), "hyperlink" ) ), "."; |
home—lects—hws
D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Feb.26 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |