Forum Moderators: coopster

Message Too Old, No Replies

html checkboxes + php sendmail problem

checkboxes w same name not all parsed in php form

         

SilverLining

10:55 am on Oct 26, 2005 (gmt 0)

10+ Year Member



Hi

on my html form i have six checkboxes with the same name but each have different values

Code extract from html form
<code>
<input type="checkbox" name="blah" value="1">1<br />
<input type="checkbox" name="blah" value="2">2<br />
<input type="checkbox" name="blah" value="3">3<br />
<input type="checkbox" name="blah" value="4">4<br />
<input type="checkbox" name="blah" value="5">5<br />
<input type="checkbox" name="blah" value="6">6<br />
</code>

The form action goes through to php page.

Code extract from php
<code>
<?
if (!isset($blah)) $blah= array();
foreach ($blah as $value)
{
$blah_req .= "Blah: ".$value."\n";
}
?>
</code>

I need to list all the checkboxes that have been ticked in the email. Not sure if global variables have been changed in PHP 5. The problem is that the checkboxes are not listed in the email sent.

Please advise... hope this makes sense :-) Not a php guru.

Thanks in advance!

dcrombie

11:12 am on Oct 26, 2005 (gmt 0)



You need to change 'blah' to 'blah[]' ;)

SilverLining

1:37 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



thanks dcrombie... havent had a chance to test yet. like this?

if (!isset($blah[])) $blah[]= array();
foreach ($blah[] as $value)
{
$blah_req .= "Blah: ".$value."\n";
}

dcrombie

3:11 pm on Oct 26, 2005 (gmt 0)



You need to change 'blah' to 'blah[]'

NOT $blah to $blah[]

;)

SilverLining

4:44 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



hey - please can you be a *little* more specific. im a complete dumbass when it comes to php. thank you kindly!

H2O_aa

5:46 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I believe what dcrombie means is that

<input type="checkbox" name="blah" value="1">1<br />
<input type="checkbox" name="blah" value="2">2<br />
<input type="checkbox" name="blah" value="3">3<br />
<input type="checkbox" name="blah" value="4">4<br />
<input type="checkbox" name="blah" value="5">5<br />
<input type="checkbox" name="blah" value="6">6<br />

instead of name="blah", use name="blah[]" and do the same thing for all of them.

dmmh

5:50 am on Oct 27, 2005 (gmt 0)

10+ Year Member



dcrombie is right, what this does is tell the form the elements are an array, which is exactly what it is, but you dont tell the form it is, so you can not process it properly

look up part 3 here for more info on html input arrays:
[zend.com...]

dmmh

5:55 am on Oct 27, 2005 (gmt 0)

10+ Year Member



you can process in a number of ways btw, I always use this:

$num_blah = count($_POST['blah']);
for ($i=0; $i<$num_blah; $i++){?>
Blah: <? echo $_POST['blah'][$i];
}

SilverLining

8:54 am on Oct 27, 2005 (gmt 0)

10+ Year Member



thanks everybody. i didnt think one could name something like that[] in html :-) life without webmasterworld just wouldnt be the same!

coopster

3:25 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, that's a common question in the PHP-to-HTML realm. The Zend link provided is right out of the PHP manual [php.net], but often overlooked.

As far as the naming convention goes, it is perfectly legal. There was a discussion once in the Javascript Forum in regards to using square brackets as part of an HTML name attribute. If you are interested in a bit more technical detail, here it is -- [webmasterworld.com...]

dmmh

8:03 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



nobody said it wasnt legal, there is just no way for PHP to distinguish/ process the element correctly if you dont set it as a HTML array ;)