PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > problem with foreach
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem with foreach

Réponse
 
LinkBack Outils de la discussion
Vieux 22/10/2007, 18h01   #1
Adam Williams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut problem with foreach

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 googled some checkbox/foreach pages on google, but I don't see where
I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
Linux. Any ?
  Réponse avec citation
Vieux 22/10/2007, 18h03   #2
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach


On Oct 22, 2007, at 1:01 PM, 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 googled some checkbox/foreach pages on google, but I don't see
> where I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on
> Fedora Linux. Any ?


Not sure if this would or not but when ever I've done anything
with $_POST variables or other array variables I have to specify it
like $_POST['option'] notice the ' in around the word 'option'

Hope it s!


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@raoset.com
  Réponse avec citation
Vieux 22/10/2007, 18h07   #3
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

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 googled some checkbox/foreach pages on google, but I don't see where
> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
> Linux. Any ?


Turn notices on. You will then get lots of notices about the use of an
undefined constant "option".

You should be using $_POST['option'] instead - notice the quotes.

Your HTML should really have double quotes around the attributes but
that's beyond the scope of this list.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 22/10/2007, 18h13   #4
Andrew Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On 10/22/07, Adam Williams <awilliam@mdah.state.ms.us> 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 googled some checkbox/foreach pages on google, but I don't see where
> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
> Linux. Any ?


A couple changes:

echo "you selected: <br>";

// Check to see that the value exists. If no options are checked, this
will be false.
if (array_key_exists('option', $_POST)) {
// include the quotes as already mentioned
foreach ($_POST['option'] as $a)
{
echo $a;
}
} else {
echo 'nothing';
}
  Réponse avec citation
Vieux 22/10/2007, 18h15   #5
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On Mon, 2007-10-22 at 18:07 +0100, Stut wrote:
> 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 googled some checkbox/foreach pages on google, but I don't see where
> > I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
> > Linux. Any ?

>
> Turn notices on. You will then get lots of notices about the use of an
> undefined constant "option".
>
> You should be using $_POST['option'] instead - notice the quotes.
>
> Your HTML should really have double quotes around the attributes but
> that's beyond the scope of this list.


He's still going to get an invalid argument warning though since PHP
will automatically convert the unquoted key to a string and then look up
the value. The problem is that the value doesn't exist or is not an
array as expected.

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Vieux 22/10/2007, 18h20   #6
Adam Williams
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach



Robert Cummings wrote:
> On Mon, 2007-10-22 at 18:07 +0100, Stut wrote:
>
>> 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 googled some checkbox/foreach pages on google, but I don't see where
>>> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
>>> Linux. Any ?
>>>

>> Turn notices on. You will then get lots of notices about the use of an
>> undefined constant "option".
>>
>> You should be using $_POST['option'] instead - notice the quotes.
>>
>> Your HTML should really have double quotes around the attributes but
>> that's beyond the scope of this list.
>>

>
> He's still going to get an invalid argument warning though since PHP
> will automatically convert the unquoted key to a string and then look up
> the value. The problem is that the value doesn't exist or is not an
> array as expected.
>
> Cheers,
> Rob.
>


Yeah, thats the problem I'm having for some reason. When I do:

print_r($_POST['option']);

it prints nothing.
  Réponse avec citation
Vieux 22/10/2007, 18h23   #7
Jay Blanchard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] problem with foreach

[snip]
print_r($_POST['option']);

it prints nothing.
[/snip]

Try resetting the array first, you may be at the end of the array;

reset($_POST);
print_r($_POST);
  Réponse avec citation
Vieux 22/10/2007, 18h51   #8
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On Mon, 2007-10-22 at 12:20 -0500, Adam Williams wrote:
>
> Yeah, thats the problem I'm having for some reason. When I do:
>
> print_r($_POST['option']);
>
> it prints nothing.


It's usually more informative to see everything that was posted so you
might have more of an idea what may have went wrong:

print_r( $_POST );

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Vieux 22/10/2007, 18h57   #9
Ludovic André
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

Adam Williams a écrit :
> I have an html page with checkboxes:
> [...]
> 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 googled some checkbox/foreach pages on google, but I don't see where
> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
> Linux. Any ?
>

Simple question: did you check any of the boxes before submitting? If
not, then it's the reason of this error... The array does not appear in
the posted variables as a not-checked checkbox is not submitted along.
But all the other comments of this thread are to be taken into account
as well ( $_POST['option'], trying to print_r($_POST), ...)

Ludovic André
  Réponse avec citation
Vieux 22/10/2007, 20h06   #10
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

Robert Cummings wrote:
> On Mon, 2007-10-22 at 18:07 +0100, Stut wrote:
>> 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 googled some checkbox/foreach pages on google, but I don't see where
>>> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora
>>> Linux. Any ?

>> Turn notices on. You will then get lots of notices about the use of an
>> undefined constant "option".
>>
>> You should be using $_POST['option'] instead - notice the quotes.
>>
>> Your HTML should really have double quotes around the attributes but
>> that's beyond the scope of this list.

>
> He's still going to get an invalid argument warning though since PHP
> will automatically convert the unquoted key to a string and then look up
> the value. The problem is that the value doesn't exist or is not an
> array as expected.


Good point. Has the OP checked any of the boxes? If not then that array
will not exist in $_POST.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 22/10/2007, 20h12   #11
Philip Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On 10/22/07, Adam Williams <awilliam@mdah.state.ms.us> 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>



Change to: <input type="checkbox" name="option[]" value="..." />...<br/>

Someone mentioned this earlier, but I thought I'd reiterate. I'm not sure
how the browser will deal with *arrays* that aren't quoted. It's very good
practice to explicitly tell what is what - don't let the browser interpret
as it wants to. (Side note: the / at the ends are for XHTML - they're
optional if only using HTML.)

~Philip

  Réponse avec citation
Vieux 23/10/2007, 00h59   #12
Bastien Koert
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] problem with foreach


I am sure that I am late to the party, but am sure that double or single quotes at least are needed around the attribute values to make this work


Civil Rights


bastien





----------------------------------------> Date: Mon, 22 Oct 2007 12:20:55 -0500> From: awilliam@mdah.state.ms.us> CC: php-general@lists.php.net> Subject: Re: [php] problem with foreach>>>> Robert Cummings wrote:>> On Mon, 2007-10-22 at 18:07 +0100, Stut wrote:>>>>> Adam Williams wrote:>>>>>>> I havean html page with checkboxes:>>>>>>>> >>>> Modern>>>> Mississippi>>>> Civil Rights>>>> MilitaryHistory>>>> >>>>>>>> and mailform2.php containing:>>>>>>>> echo "you selected: ";>>>> /* line 81 */ foreach ($_POST[option] as $a)>>>> {>>>> echo "$a";>>>> }>>>>>>>> but I'm getting the error:>>>>>>>> youselected:>>>>>>>> *Warning*: Invalid argument supplied for foreach() in>>>> */var/www/sites/mdah-test/museum/mmhsurvey/mailform2.php* on line *81*>>>>>>>> I googled some checkbox/foreach pages on google, but I don't see where>>>> I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on Fedora>>>> Linux. Any ?>>>>>>> Turn notices on. You will then get lots of notices about the use of an>>> undefined constant "option".>>>>>> You should be using $_POST['option'] instead - notice the quotes.>>>>>> Your HTML should really have double quotes around the attributes but>>> that's beyond the scopeof this list.>>>>>>> He's still going to get an invalid argument warning though since PHP>> will automatically convert the unquoted key to a string and then look up>> the value. The problem is that the value doesn't exist oris not an>> array as expected.>>>> Cheers,>> Rob.>>>> Yeah, thats the problem I'm having for some reason. When I do:>> print_r($_POST['option']);>> it prints nothing.>> --> PHP General Mailing List (http://www.php.net/)> To unsubscribe, visit: http://www.php.net/unsub.php>

__________________________________________________ _______________
Are you ready for Windows Live Messenger Beta 8.5 ? Get the latest for freetoday!
http://entertainment.sympatico.msn.ca/WindowsLiveMessenger
  Réponse avec citation
Vieux 23/10/2007, 01h36   #13
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] problem with foreach

On Mon, 2007-10-22 at 19:59 -0400, Bastien Koert wrote:
> I am sure that I am late to the party, but am sure that
> double or single quotes at least are needed around the
> attribute values to make this work


Late to the party and completely off the mark taboot. Don't need quotes
at all. It's bad practice to omit them, but it works fine.

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
  Réponse avec citation
Vieux 23/10/2007, 16h45   #14
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

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 googled some checkbox/foreach pages on google, but I don't see
>where I'm going wrong. I'm running php 5.2.5 on Apache 2.2.4 on
>Fedora Linux. Any ?



There's nothing wrong with your php code, but there is something
wrong with your html and methodology.

First, always use quotes in html for attributes and values (i.e.,
type="checkbox" value ="Submit").

Second, always plan for what the user may do (IOW clean your input).
If "none of the above" is an acceptable answer, then plan for it.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 23/10/2007, 17h05   #15
Andrew Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] problem with foreach

On 10/23/07, tedd <tedd.sperling@gmail.com> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 04h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,39894 seconds with 23 queries