|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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'; } |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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! .................................................. .......... |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
[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); |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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! .................................................. .......... |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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é |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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! .................................................. .......... |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|