Re: problem with foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/23/07, tedd <tedd.sperling@xxxxxxxxx> wrote:
> At 12:01 PM -0500 10/22/07, Adam Williams wrote:
> >I have an html page with checkboxes:
> >
> ><form action=mailform2.php method=POST>
> ><input type=checkbox name=option[] value="Modern Mississippi">Modern
> >Mississippi<br>
> ><input type=checkbox name=option[] value="Civil Rights">Civil Rights<br>
> ><input type=checkbox name=option[] value="Military
> >History">MilitaryHistory<br>
> ><input type=submit name=submit value=Submit>
> >
> >and mailform2.php containing:
> >
> >echo "you selected: <br>";
> >/* line 81 */ foreach ($_POST[option] as $a)
> >        {
> >        echo "$a";
> >        }
> >
> >but I'm getting the error:
> >
> >you selected:
> >
> >*Warning*: Invalid argument supplied for foreach() in
> >*/var/www/sites/mdah-test/museum/mmhsurvey/mailform2.php* on line
> >*81*
> >

[I just realized that when I hit reply yesterday I forgot to switch
the addresses around it sent it directly to the OP instead of the
list. Not that there is anything earth shattering here.]

I usually run $_GET, $_POST, etc. through array_key_exists before
using the value to prevent any warnings that happen and for better
flow control.

/**
Check to see that at least one value for 'option' exists. If no
options are checked, this will be false.
 */
if (array_key_exists('option', $_POST)) {
   // include the quotes as already mentioned
    echo "you selected: <br>";
   foreach ($_POST['option'] as $a)
   {
       echo $a;
   }
}
else
{
  echo 'you did not select any options.';
}


Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux