 | | Garayed.com > PHP |
Form helper issues "Invalid argument supplied for foreach()"
| | |  | Form helper issues "Invalid argument supplied for foreach()" |  | 
03-21-2008, 05:32 PM
| | | Form helper issues "Invalid argument supplied for foreach()" 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:
<?php input_select('main_dish', $defaults,
$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
<?php
// form_helpers.php
//print a text box
function input_text($element_name, $values) {
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '">';
}
//print a submit button
function input_submit($element_name, $label) {
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) .'"/>';
}
//print a textarea
function input_textarea($element_name, $values) {
print '<textarea name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}
//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values,
$element_value) {
print '<input type="' . $type . '" name="' . $element_name .'"
value="' . $element_value . '" ';
if ($element_value == $values[$element_name]) {
print ' checked="checked"';
}
print '/>';
}
//print a <select> menu
function input_select($element_name, $selected, $options, $multiple =
false) {
// print out the <select> tag
print '<select name="' . $element_name;
// if multiple choices are permitted, add the multiple attribute
// and add a [ ] to the end of the tag name
if ($multiple) { print '[]" multiple="multiple'; }
print '">';
// set up the list of things to be selected
$selected_options = array();
if ($multiple) {
foreach ($selected[$element_name] as $val) {
$selected_options[$val] = true;
}
} else {
$selected_options[ $selected[$element_name] ] = true;
}
// print out the <option> tags
foreach ($options as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if ($selected_options[$option]) {
print ' selected="selected"';
}
print '>' . htmlentities($label) . '</option>';
}
print '</select>';
}
?>
<?php
// form_meals.php
require ('form_helpers.php');
// don't forget to include the code for the form
// helper functions defined in Example 6-29
//
// setup the arrays of choices in the select menus
// these are needed in display_form(), validate_form(),
// and process_form(), so they are declared in the global scope
$sweets = array('puff' => 'Sesame Seed Puff',
'square' => 'Coconut Milk Gelatin Square',
'cake' => 'Brown Sugar Cake',
'ricemeat' => 'Sweet Rice and Meat');
$main_dishes = array('cuke' => 'Braised Sea Cucumber',
'stomach' => "Sauteed Pig's Stomach",
'tripe' => 'Sauteed Tripe with Wine Sauce',
'taro' => 'Stewed Pork with Taro',
'giblets' => 'Baked Giblets with Salt',
'abalone' => 'Abalone with Marrow and Duck
Feet');
// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}
function show_form($errors = '') {
// If the form is submitted, get defaults from submitted
parameters
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// Otherwise, set our own defaults: medium size and yes to
delivery
$defaults = array('delivery' => 'yes',
'size' => 'medium');
}
// If errors were passed in, put them in $error_text (with HTML
markup)
if ($errors) {
$error_text = '<tr><td>You need to correct the following
errors:';
$error_text .= '</td><td><ul><li>';
$error_text .= implode('</li><li>',$errors);
$error_text .= '</li></ul></td></tr>';
} else {
// No errors? Then $error_text is blank
$error_text = '';
}
// Jump out of PHP mode to make displaying all the HTML tags
easier
?>
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table>
<?php print $error_text ?>
<tr><td>Your Name:</td>
<td><?php input_text('name', $defaults) ?></td></tr>
<tr><td>Size:</td>
<td><?php input_radiocheck('radio','size', $defaults, 'small'); ?>
Small <br/>
<?php input_radiocheck('radio','size', $defaults, 'medium'); ?> Medium
<br/>
<?php input_radiocheck('radio','size', $defaults, 'large'); ?> Large
</td></tr>
<tr><td>Pick one sweet item:</td>
<td><?php input_select('sweet', $defaults, $GLOBALS['sweets']); ?>
</td></tr>
<tr><td>Pick two main dishes:</td>
<td>
<?php input_select('main_dish', $defaults, $GLOBALS['main_dishes'],
true) ?>
</td></tr>
<tr><td>Do you want your order delivered?</td>
<td><?php input_radiocheck('checkbox','delivery', $defaults, 'yes'); ?
> Yes
</td></tr>
<tr><td>Enter any special instructions.<br/>
If you want your order delivered, put your address here:</td>
<td><?php input_textarea('comments', $defaults); ?></td></tr>
<tr><td colspan="2" align="center"><?php
input_submit('save','Order'); ?>
</td></tr>
</table>
<input type="hidden" name="_submit_check" value="1"/>
</form>
<?php
} // 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 <br/> tags
print nl2br(htmlentities($message));
}
?> |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-22-2008, 06:21 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> <?php input_select('main_dish', $defaults,
> $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 '<option value="' . htmlentities($option) . '"';
....which could be rewritten as...
echo '<option value="', htmlentities($option), '"';
--
Michael Placentra II |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-29-2008, 09:44 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On 22 Mar, 18:21, Mike Placentra II
<nothingsoriginalontheinter...@gmail.com> wrote:
> On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> > <?php input_select('main_dish', $defaults,
> > $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 '<option value="' . htmlentities($option) . '"';
> ...which could be rewritten as...
> echo '<option value="', htmlentities($option), '"';
>
> --
> Michael Placentra II
It turns out that the problem lies with the array of default values
named, suprisingly, $defaults. There were no default values for the
main_dish, which must have caused the error. I managed to get it
working by adding two selections for the main_dish to the $defaults
array:
<?php
//right
$defaults = array('delivery' => 'yes',
'size' => 'medium',
'main_dish' => array('cuke', 'tripe',
'checkbox' => 'yes'));
var_dump($defaults);
echo "<br>";
echo "<br>";
Thanks for the PHP 5 pointers, by the way. Much appreciated! |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 10:45 AM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On 29 Mar, 22:44, mejpark <mejp...@gmail.com> wrote:
> On 22 Mar, 18:21, Mike Placentra II
>
>
>
> <nothingsoriginalontheinter...@gmail.com> wrote:
> > On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> > > <?php input_select('main_dish', $defaults,
> > > $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 '<option value="' . htmlentities($option) . '"';
> > ...which could be rewritten as...
> > echo '<option value="', htmlentities($option), '"';
>
> > --
> > Michael Placentra II
>
> It turns out that the problem lies with the array of default values
> named, suprisingly, $defaults. There were no default values for the
> main_dish, which must have caused the error. I managed to get it
> working by adding two selections for the main_dish to the $defaults
> array:
>
> <?php
>
> //right
> $defaults = array('delivery' => 'yes',
> 'size' => 'medium',
> 'main_dish' => array('cuke', 'tripe',
> 'checkbox' => 'yes'));
>
> var_dump($defaults);
> echo "<br>";
> echo "<br>";
>
> Thanks for the PHP 5 pointers, by the way. Much appreciated!
When the script is executed on my production web server, the following
notices are logged:
PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/forms.php on line 48
PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/forms.php on line 78
PHP Notice: Undefined index: name in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 8
PHP Notice: Undefined index: sweet in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 73
PHP Notice: Undefined index: puff in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: square in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: cake in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: ricemeat in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: stomach in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: taro in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: giblets in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: abalone in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
PHP Notice: Undefined index: comments in /home/fhlinux150/s/swf-
games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 32
? |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 10:49 AM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On 29 Mar, 22:44, mejpark <mejp...@gmail.com> wrote:
> On 22 Mar, 18:21, Mike Placentra II
>
>
>
> <nothingsoriginalontheinter...@gmail.com> wrote:
> > On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> > > <?php input_select('main_dish', $defaults,
> > > $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 '<option value="' . htmlentities($option) . '"';
> > ...which could be rewritten as...
> > echo '<option value="', htmlentities($option), '"';
>
> > --
> > Michael Placentra II
>
> It turns out that the problem lies with the array of default values
> named, suprisingly, $defaults. There were no default values for the
> main_dish, which must have caused the error. I managed to get it
> working by adding two selections for the main_dish to the $defaults
> array:
>
> <?php
>
> //right
> $defaults = array('delivery' => 'yes',
> 'size' => 'medium',
> 'main_dish' => array('cuke', 'tripe',
> 'checkbox' => 'yes'));
>
> var_dump($defaults);
> echo "<br>";
> echo "<br>";
>
> Thanks for the PHP 5 pointers, by the way. Much appreciated!
When the script is executed on my production server, the following
notices are logged:
PHP Notice: Undefined index: _submit_check in /path/to/file/
forms.php on line 48
PHP Notice: Undefined index: _submit_check in /path/to/file/
forms.php on line 78
PHP Notice: Undefined index: name in /path/to/file/formhelpers.php
on line 8
PHP Notice: Undefined index: sweet in /path/to/file/formhelpers.php
on line 73
PHP Notice: Undefined index: puff in /path/to/file/formhelpers.php
on line 78
PHP Notice: Undefined index: square in /path/to/file/formhelpers.php
on line 78
PHP Notice: Undefined index: cake in /path/to/file/formhelpers.php
on line 78
PHP Notice: Undefined index: ricemeat in /path/to/file/
formhelpers.php on line 78
PHP Notice: Undefined index: stomach in /path/to/file/
formhelpers.php on line 78
PHP Notice: Undefined index: taro in /path/to/file/formhelpers.php
on line 78
PHP Notice: Undefined index: giblets in /path/to/file/
formhelpers.php on line 78
PHP Notice: Undefined index: abalone in /path/to/file/
formhelpers.php on line 78
PHP Notice: Undefined index: comments in /path/to/file/
formhelpers.php on line 32
Why is this? I don't have much faith in O'Reilly's code right now! |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 01:47 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" mejpark wrote:
> On 29 Mar, 22:44, mejpark <mejp...@gmail.com> wrote:
>> On 22 Mar, 18:21, Mike Placentra II
>>
>>
>>
>> <nothingsoriginalontheinter...@gmail.com> wrote:
>>> On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
>>>> <?php input_select('main_dish', $defaults,
>>>> $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 '<option value="' . htmlentities($option) . '"';
>>> ...which could be rewritten as...
>>> echo '<option value="', htmlentities($option), '"';
>>> --
>>> Michael Placentra II
>> It turns out that the problem lies with the array of default values
>> named, suprisingly, $defaults. There were no default values for the
>> main_dish, which must have caused the error. I managed to get it
>> working by adding two selections for the main_dish to the $defaults
>> array:
>>
>> <?php
>>
>> //right
>> $defaults = array('delivery' => 'yes',
>> 'size' => 'medium',
>> 'main_dish' => array('cuke', 'tripe',
>> 'checkbox' => 'yes'));
>>
>> var_dump($defaults);
>> echo "<br>";
>> echo "<br>";
>>
>> Thanks for the PHP 5 pointers, by the way. Much appreciated!
>
> When the script is executed on my production web server, the following
> notices are logged:
> PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/forms.php on line 48
> PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/forms.php on line 78
> PHP Notice: Undefined index: name in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 8
> PHP Notice: Undefined index: sweet in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 73
> PHP Notice: Undefined index: puff in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: square in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: cake in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: ricemeat in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: stomach in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: taro in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: giblets in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: abalone in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> PHP Notice: Undefined index: comments in /home/fhlinux150/s/swf-
> games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 32
>
> ?
>
You never posted the script as requested, so we have no idea what's
going on.
If you want help, you need to post enough information for us to help
you. We're not a library or a book store, and very few of us have any
specific book.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. jstucklex@attglobal.net
================== |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 03:16 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On Mar 30, 2:47 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> mejpark wrote:
> > On 29 Mar, 22:44, mejpark <mejp...@gmail.com> wrote:
> >> On 22 Mar, 18:21, Mike Placentra II
>
> >> <nothingsoriginalontheinter...@gmail.com> wrote:
> >>> On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> >>>> <?php input_select('main_dish', $defaults,
> >>>> $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 '<option value="' . htmlentities($option) . '"';
> >>> ...which could be rewritten as...
> >>> echo '<option value="', htmlentities($option), '"';
> >>> --
> >>> Michael Placentra II
> >> It turns out that the problem lies with the array of default values
> >> named, suprisingly, $defaults. There were no default values for the
> >> main_dish, which must have caused the error. I managed to get it
> >> working by adding two selections for the main_dish to the $defaults
> >> array:
>
> >> <?php
>
> >> //right
> >> $defaults = array('delivery' => 'yes',
> >> 'size' => 'medium',
> >> 'main_dish' => array('cuke', 'tripe',
> >> 'checkbox' => 'yes'));
>
> >> var_dump($defaults);
> >> echo "<br>";
> >> echo "<br>";
>
> >> Thanks for the PHP 5 pointers, by the way. Much appreciated!
>
> > When the script is executed on my production web server, the following
> > notices are logged:
> > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 48
> > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 78
> > PHP Notice: Undefined index: name in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 8
> > PHP Notice: Undefined index: sweet in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 73
> > PHP Notice: Undefined index: puff in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: square in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: cake in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: ricemeat in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: stomach in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: taro in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: giblets in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: abalone in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > PHP Notice: Undefined index: comments in /home/fhlinux150/s/swf-
> > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 32
>
> > ?
>
> You never posted the script as requested, so we have no idea what's
> going on.
>
> If you want help, you need to post enough information for us to help
> you. We're not a library or a book store, and very few of us have any
> specific book.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
I've only modified the defaults array--the rest of the code is in
tact. Here it is:
==================
<?php
/*
* formhelpers.php
*/
//print a text box
function input_text($element_name, $values) {
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '">';
}
//print a submit button
function input_submit($element_name, $label) {
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) .'"/>';
}
//print a textarea
function input_textarea($element_name, $values) {
print '<textarea name="' . $element_name .'">';
print htmlentities($values[$element_name]) . '</textarea>';
}
//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values,
$element_value) {
print '<input type="' . $type . '" name="' . $element_name .'"
value="' . $element_value . '" ';
if ($element_value == $values[$element_name]) {
print ' checked="checked"';
}
print '/>';
}
//print a <select> menu
function input_select($element_name, $selected, $options, $multiple =
false) {
// print out the <select> tag
print '<select name="' . $element_name;
// if multiple choices are permitted, add the multiple attribute
// and add a [ ] to the end of the tag name
if ($multiple) { print '[]" multiple="multiple'; }
print '">';
// set up the list of things to be selected
$selected_options = array( );
if ($multiple) {
foreach ($selected[$element_name] as $val) {
$selected_options[$val] = true;
}
} else {
$selected_options[$selected[$element_name]] = true;
}
// print out the <option> tags
foreach ($options as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if ($selected_options[$option]) {
print ' selected="selected"';
}
print '>' . htmlentities($label) . '</option>';
}
print '</select>';
}
?>
==================
<?php
/*
* form_meals.php
*/
include('formhelpers.php');
ini_set('display_errors','0');
// don't forget to include the code for the form
// helper functions defined in Example 6-29
// setup the arrays of choices in the select menus
// these are needed in display_form( ), validate_form( ),
// and process_form( ), so they are declared in the global scope
$sweets = array('puff' => 'Sesame Seed Puff',
'square' => 'Coconut Milk Gelatin Square',
'cake' => 'Brown Sugar Cake',
'ricemeat' => 'Sweet Rice and Meat');
$main_dishes = array('cuke' => 'Braised Sea Cucumber',
'stomach' => "Sauteed Pig's Stomach",
'tripe' => 'Sauteed Tripe with Wine Sauce',
'taro' => 'Stewed Pork with Taro',
'giblets' => 'Baked Giblets with Salt',
'abalone' => 'Abalone with Marrow and Duck
Feet');
// The main page logic:
// - If the form is submitted, validate and then process or redisplay
// - If it's not submitted, display
if ($_POST['_submit_check']) {
// If validate_form( ) returns errors, pass them to show_form( )
if ($form_errors = validate_form( )) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form( );
}
} else {
// The form wasn't submitted, so display
show_form( );
}
function show_form($errors = '') {
// If the form is submitted, get defaults from submitted
parameters
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// Otherwise, set our own defaults: medium size and yes to
delivery
$defaults = array('delivery' => 'yes',
'size' => 'medium',
'main_dish' => array('cuke', 'tripe'),
'checkbox' => 'yes');
}
// If errors were passed in, put them in $error_text (with HTML
markup)
if ($errors) {
$error_text = '<tr><td>You need to correct the following
errors:';
$error_text .= '</td><td><ul><li>';
$error_text .= implode('</li><li>',$errors);
$error_text .= '</li></ul></td></tr>';
} else {
// No errors? Then $error_text is blank
$error_text = '';
}
// Jump out of PHP mode to make displaying all the HTML tags
easier
?>
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<table>
<?php print $error_text ?>
<tr><td>Your Name:</td>
<td><?php input_text('name', $defaults) ?></td></tr>
<tr><td>Size:</td>
<td><?php input_radiocheck('radio','size', $defaults, 'small'); ?>
Small <br/>
<?php input_radiocheck('radio','size', $defaults, 'medium'); ?> Medium
<br/>
<?php input_radiocheck('radio','size', $defaults, 'large'); ?> Large
</td></tr>
<tr><td>Pick one sweet item:</td>
<td><?php input_select('sweet', $defaults, $GLOBALS['sweets']); ?>
</td></tr>
<tr><td>Pick two main dishes:</td>
<td>
<?php input_select('main_dish', $defaults, $GLOBALS['main_dishes'],
true) ?>
</td></tr>
<tr><td>Do you want your order delivered?</td>
<td><?php input_radiocheck('checkbox','delivery', $defaults, 'yes'); ?
> Yes
</td></tr>
<tr><td>Enter any special instructions.<br/>
If you want your order delivered, put your address here:</td>
<td><?php input_textarea('comments', $defaults); ?></td></tr>
<tr><td colspan="2" align="center"><?php
input_submit('save','Order'); ?>
</td></tr>
</table>
<input type="hidden" name="_submit_check" value="1"/>
</form>
<?php
} // 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 <br/> tags
print nl2br(htmlentities($message));
}
?>
================== |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 03:52 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" On Mar 30, 6:16 pm, mejpark <mejp...@gmail.com> wrote:
> On Mar 30, 2:47 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>
>
>
> > mejpark wrote:
> > > On 29 Mar, 22:44, mejpark <mejp...@gmail.com> wrote:
> > >> On 22 Mar, 18:21, Mike Placentra II
>
> > >> <nothingsoriginalontheinter...@gmail.com> wrote:
> > >>> On Mar 21, 1:32 pm, mejpark <mejp...@gmail.com> 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:
> > >>>> <?php input_select('main_dish', $defaults,
> > >>>> $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 ina
> > >>> 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 '<option value="' . htmlentities($option) . '"';
> > >>> ...which could be rewritten as...
> > >>> echo '<option value="', htmlentities($option), '"';
> > >>> --
> > >>> Michael Placentra II
> > >> It turns out that the problem lies with the array of default values
> > >> named, suprisingly, $defaults. There were no default values for the
> > >> main_dish, which must have caused the error. I managed to get it
> > >> working by adding two selections for the main_dish to the $defaults
> > >> array:
>
> > >> <?php
>
> > >> //right
> > >> $defaults = array('delivery' => 'yes',
> > >> 'size' => 'medium',
> > >> 'main_dish' => array('cuke', 'tripe',
> > >> 'checkbox' => 'yes'));
>
> > >> var_dump($defaults);
> > >> echo "<br>";
> > >> echo "<br>";
>
> > >> Thanks for the PHP 5 pointers, by the way. Much appreciated!
>
> > > When the script is executed on my production web server, the following
> > > notices are logged:
> > > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 48
> > > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 78
> > > PHP Notice: Undefined index: name in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 8
> > > PHP Notice: Undefined index: sweet in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 73
> > > PHP Notice: Undefined index: puff in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: square in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: cake in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: ricemeat in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: stomach in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: taro in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: giblets in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: abalone in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: comments in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 32
>
> > > ?
>
> > You never posted the script as requested, so we have no idea what's
> > going on.
>
> > If you want help, you need to post enough information for us to help
> > you. We're not a library or a book store, and very few of us have any
> > specific book.
>
> > --
> > ==================
> > Remove the "x" from my email address
> > Jerry Stuckle
> > JDS Computer Training Corp.
> > jstuck...@attglobal.net
> > ==================
>
> I've only modified the defaults array--the rest of the code is in
> tact. Here it is:
> ==================
> <?php
> /*
> * formhelpers.php
> */
> //print a text box
> function input_text($element_name, $values) {
> print '<input type="text" name="' . $element_name .'" value="';
> print htmlentities($values[$element_name]) . '">';}
>
> //print a submit button
> function input_submit($element_name, $label) {
> print '<input type="submit" name="' . $element_name .'" value="';
> print htmlentities($label) .'"/>';}
>
> //print a textarea
> function input_textarea($element_name, $values) {
> print '<textarea name="' . $element_name .'">';
> print htmlentities($values[$element_name]) . '</textarea>';}
>
> //print a radio button or checkbox
> function input_radiocheck($type, $element_name, $values,
> $element_value) {
> print '<input type="' . $type . '" name="' . $element_name .'"
> value="' . $element_value . '" ';
> if ($element_value == $values[$element_name]) {
> print ' checked="checked"';
> }
> print '/>';}
>
> //print a <select> menu
> function input_select($element_name, $selected, $options, $multiple =
> false) {
> // print out the <select> tag
> print '<select name="' . $element_name;
> // if multiple choices are permitted, add the multiple attribute
> // and add a [ ] to the end of the tag name
> if ($multiple) { print '[]" multiple="multiple'; }
> print '">';
> // set up the list of things to be selected
> $selected_options = array( );
> if ($multiple) {
> foreach ($selected[$element_name] as $val) {
> $selected_options[$val] = true;
> }
> } else {
> $selected_options[$selected[$element_name]] = true;
> }
> // print out the <option> tags
> foreach ($options as $option => $label) {
> print '<option value="' . htmlentities($option) . '"';
> if ($selected_options[$option]) {
> print ' selected="selected"';
> }
> print '>' . htmlentities($label) . '</option>';
> }
> print '</select>';}
>
> ?>
> ==================
> <?php
> /*
> * form_meals.php
> */
> include('formhelpers.php');
> ini_set('display_errors','0');
> // don't forget to include the code for the form
> // helper functions defined in Example 6-29
> // setup the arrays of choices in the select menus
> // these are needed in display_form( ), validate_form( ),
> // and process_form( ), so they are declared in the global scope
> $sweets = array('puff' => 'Sesame Seed Puff',
> 'square' => 'Coconut Milk Gelatin Square',
> 'cake' => 'Brown Sugar Cake',
> 'ricemeat' => 'Sweet Rice and Meat');
>
> $main_dishes = array('cuke' => 'Braised Sea Cucumber',
> 'stomach' => "Sauteed Pig's Stomach",
> 'tripe' => 'Sauteed Tripe with Wine Sauce',
> 'taro' => 'Stewed Pork with Taro',
> 'giblets' => 'Baked Giblets with Salt',
> 'abalone' => 'Abalone with Marrow and Duck
> Feet');
> // The main page logic:
> // - If the form is submitted, validate and then process or redisplay
> // - If it's not submitted, display
> if ($_POST['_submit_check']) {
> // If validate_form( ) returns errors, pass them to show_form( )
> if ($form_errors = validate_form( )) {
> show_form($form_errors);
> } else {
> // The submitted data is valid, so process it
> process_form( );
> }} else {
>
> // The form wasn't submitted, so display
> show_form( );
>
> }
>
> function show_form($errors = '') {
> // If the form is submitted, get defaults from submitted
> parameters
> if ($_POST['_submit_check']) {
> $defaults = $_POST;
> } else {
> // Otherwise, set our own defaults: medium size and yes to
> delivery
> $defaults = array('delivery' => 'yes',
> 'size' => 'medium',
> 'main_dish' => array('cuke', 'tripe'),
> 'checkbox' => 'yes');
> }
> // If errors were passed in, put them in $error_text (with HTML
> markup)
> if ($errors) {
> $error_text = '<tr><td>You need to correct the following
> errors:';
> $error_text .= '</td><td><ul><li>';
> $error_text .= implode('</li><li>',$errors);
> $error_text .= '</li></ul></td></tr>';
> } else {
> // No errors? Then $error_text is blank
> $error_text = '';...
>
> read more »
There is a small catch. The code "if ($_POST['_submit_check'])" is a
wrong approach to detecting a $_POST request, because it will cause an
E_NOTICE message to be displayed on GET requests. You should use "if
($_SERVER['REQUEST_METHOD']=='POST')" instead. |  |  | Re: Form helper issues "Invalid argument supplied for foreach()" |  | 
03-30-2008, 04:54 PM
| | | Re: Form helper issues "Invalid argument supplied for foreach()" mejpark wrote:
> On Mar 30, 2:47 pm, Jerry Stuckle <jstuck...@attglobal.net> 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
================== | | Thread Tools | Search this Thread | | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | All times are GMT. The time now is 02:30 AM. | | | |