I'm in chapter 7 of Larry Ullman's PHP book and there is a simple example that I can't get to work. (All in all, the book is going fairly well.)
When you submit the form but don't check any checkboxes, this comes up...
Error:
Notice: Undefined index: weekdays in /Users/user1/Documents/DEV/+htdocs/VisualQuickstart/0709_HandleEvent.php on line 15
Call Stack
Time Memory Function Location
1 0.0006 59120 {main}( ) ../0709_HandleEvent.php:0
According the book, my ELSE clause should handle tis scenario, and when I step through the code in NetBeans, it does skip to the ELSE part since is_array = FALSE.
Here are my two files...
0708_Event.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="0709_HandleEvent.php" method="post">
<p>Event Name: <input type="text" name="name" size="30"/></p>
<p>Week Days:
<input type="checkbox" name="weekdays[]" value="Sunday"/> S
<input type="checkbox" name="weekdays[]" value="Monday"/> M
<input type="checkbox" name="weekdays[]" value="Tuesday"/> T
<input type="checkbox" name="weekdays[]" value="Wednesday"/> W
<input type="checkbox" name="weekdays[]" value="Thursday"/> R
<input type="checkbox" name="weekdays[]" value="Friday"/> F
<input type="checkbox" name="weekdays[]" value="Saturday"/> S
</p>
<input type="submit" name="submit" value="Add the Event!"/>
</form>
</body>
</html>
0709_HandleEvent.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
// Print introductory text.
print "<p>You want to add an event called <b>{$_POST['name']}</b>
which takes place on: <br />";
// Print the weekday.
if (is_array($_POST['weekdays'])) {
foreach ($_POST['weekdays'] as $day) {
print "$day<br />\n";
}
} else {
print 'Please select at least one weekday for this event!';
}
// Complete paragraph.
print '</p>';
?>
</body>
</html>
Some assistance would be nice!
Amy