POST checkbox group

Hi

Trying to send the data from a checkbox group to a php process and it's
just sending the last box clicked as a variable, rather than the whole
group as an array. Am I being stupid?

ie html:

<form name="selectform" action="data.php" method="post">
<input name="days" type="checkbox" value="mon" />Mon
<input name="days" type="checkbox" value="tue" />Tue
<input name="days" type="checkbox" value="wed" />Wed
<input type="submit" name="Submit" value="Submit" />
</form>

php:

print_r($_POST["days"])
gives
Array ( [days] => tue [Submit] => Submit )

even when Mon and Tues are checked

Thanks
--
Richard
Richard Walker [ Mi, 31 Januar 2007 13:52 ] [ ID #1614397 ]

Re: POST checkbox group

..oO(Richard Walker)

>Trying to send the data from a checkbox group to a php process and it's
>just sending the last box clicked as a variable, rather than the whole
>group as an array. Am I being stupid?
>
>ie html:
>
> <form name="selectform" action="data.php" method="post">
> <input name="days" type="checkbox" value="mon" />Mon
> <input name="days" type="checkbox" value="tue" />Tue
> <input name="days" type="checkbox" value="wed" />Wed
> <input type="submit" name="Submit" value="Submit" />
> </form>

Use name="days[]". Then $_POST['days'] will be an array, containing the
values of all checked checkboxes.

Micha
Michael Fesser [ Mi, 31 Januar 2007 14:29 ] [ ID #1614398 ]

Re: POST checkbox group

Many Thanks. Easy when you know how!
--
Richard

Michael Fesser wrote:
> .oO(Richard Walker)
>
>> Trying to send the data from a checkbox group to a php process and it's
>> just sending the last box clicked as a variable, rather than the whole
>> group as an array. Am I being stupid?
>>
>> ie html:
>>
>> <form name="selectform" action="data.php" method="post">
>> <input name="days" type="checkbox" value="mon" />Mon
>> <input name="days" type="checkbox" value="tue" />Tue
>> <input name="days" type="checkbox" value="wed" />Wed
>> <input type="submit" name="Submit" value="Submit" />
>> </form>
>
> Use name="days[]". Then $_POST['days'] will be an array, containing the
> values of all checked checkboxes.
>
> Micha
Richard Walker [ Mi, 31 Januar 2007 14:37 ] [ ID #1614399 ]

Re: POST checkbox group

..oO(Richard Walker)

> Many Thanks. Easy when you know how!

It's in the manual, but a bit hidden:

3. How do I create arrays in a HTML <form>?
http://www.php.net/manual/en/faq.html.php#faq.html.arrays

4. How do I get all the results from a select multiple HTML tag?
http://www.php.net/manual/en/faq.html.php#faq.html.select-mu ltiple

Micha
Michael Fesser [ Mi, 31 Januar 2007 14:56 ] [ ID #1614400 ]
PHP » alt.php » POST checkbox group

Vorheriges Thema: how to delete directories that are created by PHP?
Nächstes Thema: Question about a data structure