Re: Form helper issues "Invalid argument supplied for foreach()"

Re: Form helper issues "Invalid argument supplied for foreach()"

am 30.03.2008 17:16:57 von mejpark

On Mar 30, 2:47 pm, Jerry Stuckle wrote:
> mejpark wrote:
> > On 29 Mar, 22:44, mejpark wrote:
> >> On 22 Mar, 18:21, Mike Placentra II
>
> >> wrote:
> >>> On Mar 21, 1:32 pm, mejpark wrote:
> >>>> Hello,
> >>>> I'm working my way through O'Reilly's "Learning PHP 5", and I've hit a
> >>>> brick wall. There are two files involved: the first contains some
> >>>> helper functions to print HTML forms (form_helpers.php). The second
> >>>> file (form_meals.php) calls these functions to print an HTML form with
> >>>> various inputs.
> >>>> The error I'm getting is "( ! ) Warning: Invalid argument supplied for
> >>>> foreach() in form_helpers.php on line 80". This line is inside the
> >>>> function input_select, specifically the foreach statement, which is
> >>>> used to determine which elements are selected by default when the form
> >>>> is displayed.
> >>>> I think the error relates to the code that calls the input_select
> >>>> function:
> >>>> > >>>> $GLOBALS['main_dishes'], $multiple = true) ?>
> >>>> This program is taken directly from the ebook, so I cannot see why it
> >>>> doesn't work.
> >>>> Any pointers much appreciated.
> >>>> Thanks
> >>> It works for me in PHP (cli) 5.2.3 as well as PHP (cgi) 4.4.8. Are you
> >>> sure that's the exact code you were trying? Could you copy and paste
> >>> the code from your post and try it again? (there will be some problems
> >>> that need to be corrected in form_meal.php because when it was posted
> >>> the lines were word-wrapped so one-line comments were broken into
> >>> two).
> >>> This is unrelated to your problem, but since you're learning PHP5
> >>> anyway you can benefit from using...
> >>> echo "abc", $def, "ghi";
> >>> ...instead of...
> >>> print "abc" . $def . "ghi";
> >>> ...since using echo with commas in PHP5 tells it to output each string
> >>> consecutively instead of concatenating it and then outputting it all
> >>> as one (concatenating takes a little extra time, more significant in a
> >>> loop). This doesn't apply if you're not outputting the string, though,
> >>> such as when you are returning it. This would be useful for that line
> >>> in the input_select() function definition (form_helpers.php) in the
> >>> foreach loop...
> >>> print '

';
} else {
// No errors? Then $error_text is blank
$error_text = '';
}
// Jump out of PHP mode to make displaying all the HTML tags
easier
?>
















Your Name:
Size:
Small

Medium


Large
Pick one sweet item:
Pick two main dishes:
true) ?>
Do you want your order delivered? > Yes
Enter any special instructions.

If you want your order delivered, put your address here:
input_submit('save','Order'); ?>



} // The end of show_form( )
function validate_form( ) {
$errors = array( );
// name is required
if (! strlen(trim($_POST['name']))) {
$errors[ ] = 'Please enter your name.';
}
// size is required
if (($_POST['size'] != 'small') && ($_POST['size'] != 'medium') &&
($_POST['size'] != 'large')) {
$errors[ ] = 'Please select a size.';
}
// sweet is required
if (! array_key_exists($_POST['sweet'], $GLOBALS['sweets'])) {
$errors[ ] = 'Please select a valid sweet item.';
}
// exactly two main dishes required
if (count($_POST['main_dish']) != 2) {
$errors[ ] = 'Please select exactly two main dishes.';
} else {
// We know there are two main dishes selected, so make sure
they are
// both valid
if (! (array_key_exists($_POST['main_dish'][0],
$GLOBALS['main_dishes']) &&
array_key_exists($_POST['main_dish'][1],
$GLOBALS['main_dishes']))) {
$errors[ ] = 'Please select exactly two valid main
dishes.';
}
}
// if delivery is checked, then comments must contain something
if (($_POST['delivery'] == 'yes') && (!
strlen(trim($_POST['comments'])))) {
$errors[ ] = 'Please enter your address for delivery.';
}
return $errors;
}
function process_form( ) {
// look up the full names of the sweet and the main dishes in
// the $GLOBALS['sweets'] and $GLOBALS['main_dishes'] arrays
$sweet = $GLOBALS['sweets'][ $_POST['sweet'] ];
$main_dish_1 = $GLOBALS['main_dishes'][ $_POST['main_dish'][0] ];
$main_dish_2 = $GLOBALS['main_dishes'][ $_POST['main_dish'][1] ];
if ($_POST['delivery'] == 'yes') {
$delivery = 'do';
} else {
$delivery = 'do not';
}
// build up the text of the order message
$message=<<<_ORDER_
Thank you for your order, $_POST[name].
You requested the $_POST[size] size of $sweet, $main_dish_1, and
$main_dish_2.
You $delivery want delivery.
_ORDER_;
if (strlen(trim($_POST['comments']))) {
$message .= 'Your comments: '.$_POST['comments'];
}
// send the message to the chef
mail('chef@restaurant.example.com', 'New Order', $message);
// print the message, but encode any HTML entities
// and turn newlines into
tags
print nl2br(htmlentities($message));
}
?>
==================

Re: Form helper issues "Invalid argument supplied for foreach()"

am 30.03.2008 18:54:29 von Jerry Stuckle

mejpark wrote:
> On Mar 30, 2:47 pm, Jerry Stuckle wrote:
>> mejpark wrote:>
> I've only modified the defaults array--the rest of the code is in
> tact. Here it is:

Your problem is your code is checking the value of
$_POST['_submit_check'] but not checking to see if any value is set.
The first time the page is called, this will not be set.

Failing code:

if ($_POST['_submit_check']) {

Should be:

if (isset($_POST['_submit_check'])) {

Or, better yet.

if (isset($_POST['_submit_check']) && $_POST['_submit_check'] == 1) {

Normally I think O'Reilly books are pretty good - but your problems with
code makes me wonder about this one...


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================