RE: [PHP] checkbox arrays and validation
| From: | Ford, Mike | Date: | Tue, 05 Oct 2004 10:44:19 +0000 |
| Subject: | RE: [PHP] checkbox arrays and validation | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-198653@lists.php.net to get a copy of this message | ||
On 05 October 2004 11:10, Angelo Zanetti wrote:
> Hi all,
>
> this might be slightly off-topic in a way but Im looking for help not
> flaming.
>
> Ok I have a checkbox array that I populate from a DB using php:
>
> while ($row = mysql_fetch_array($result))
> echo("<input type=checkbox name=chkmodels[] value=" .
> $row['models_id'] . ">" . $row['models_type'] .
> "<br>");
>
> So when I want to see each variable in the array I use:
>
> foreach ($_POST['chkmodels'] as $c)
> {
> echo("<br> value: " . $c);
> }
>
> this is fine.
>
> but what I want to clarify is that if I change my PHP echo statement
> to:
>
> echo("<input type=checkbox name=chkmodels value=" . $row['models_id']
> . ">" . $row['models_type'] . "<br>");
>
> basically without the [] then it is still recognised as an array. But
> when the array is passed back it is returned as a string separated by
> commas.
No, this is incorrect. You will see only the last value selected, returned
as a simple scalar. PHP doesn't do automatic array-ification of multiple
values -- that's why you need the [] in the name attribute.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211