|
home—lects—exams—hws
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?
(To be discussed in a future lecture, not today:)
Note that to prevent errors, some php servers used to
enable “magic quotes”:
When setting up the
This has been deprecated and will be discontinued
(though php.radford.edu still uses them as of 2011.Sep.16).
A poor solution is to always call
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"; } |
Exercise: Let's write a function which takes in the name of a directory, and returns a string mentioning each file in the directory: something like:
"One file is someFile.txt.<br />Another file is someFile2.txt.<br />Another file is Zzzz.php.<br />"Hint1:
Exercise: Write a function that takes in an array of key/values pairs, and returning a string that happens to be html for a pull-down menu of those values.
test( HTMLPulldown( array( "Jan" => "January", "Dec" => "December" ) ), "<select <option value='Jan'>January</option> <option value='Dec'>December</option> </select>" ); test( HTMLPulldown( array( "January", "February", "December" ) ), "<select <option value='0'>January</option> <option value='1'>February</option> <option value='2'>December</option> </select>" ); |
Optional: extend the above by including an optional parameter: a top-option like “please select:”.
Left as an exercise: Similar to above, make a function that takes in an array, and returns (the html for) a bunch of checkboxes, inside table.
1Usually, a google search like “php list files” gives good matches; in this particular case we get sidetracked by related functions that deal with file-handles and are iterators. ↩
home—lects—exams—hws
breeze (snow day)
©2011, Ian Barland, Radford University Last modified 2011.Sep.19 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |