RE: [PHP] Array of CheckBoxes
| From: | John Holmes | Date: | Wed, 26 Jun 2002 02:56:27 +0000 |
| Subject: | RE: [PHP] Array of CheckBoxes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-103892@lists.php.net to get a copy of this message | ||
Hmm..only problem with this is that if a checkbox is not checked, it's
variable isn't set at all.
<input type='checkbox' name='one' value='1'>
<input type='checkbox' name='two' value='2'>
If you check 'one', you have a variable $one == 1. If you leave 'two'
blank, then you get nothing. $two isn't set at all.
So, the first part of your solution is easy, the array of values that
were checked...
<input type='checkbox' name='name[]' value='one'>
<input type='checkbox' name='name[]' value='two'>
<input type='checkbox' name='name[]' value='three'>
So when that is submitted, as long as one checkbox was checked, you'll
have a $name[] array that contains one, two, or three, based on which
ones were checked.
What you could do, if you know how many checkboxes there were supposed
to be, is use something like this...
<input type='checkbox' name='name[1]' value='one'>
<input type='checkbox' name='name[2]' value='two'>
<input type='checkbox' name='name[3]' value='three'>
the process like this:
<?
for($x=0;$x<$Num_Checkboxes;$x++)
{
if(!isset($name[$x]))
{ $name[$x] = FALSE; }
}
?>
That'll check for missing keys in the $name array and assign a false
value to them...
Anyway...I'm rambling... :)
---John Holmes...
> -----Original Message-----
> From: César Aracena [mailto:icaam@icaam.com.ar]
> Sent: Tuesday, June 25, 2002 10:38 PM
> To: PHP General List
> Subject: [PHP] Array of CheckBoxes
>
> Hi all.
>
> I have a form in which I must add a set of check boxes at the bottom.
> Once the form is passed away, the information of the checked boxes
> should be inserted into a DB. The thing is that I dont like to use
> tons of if statements cause it will have way too many check
boxes
> Is there any way of creating an Array which holds TRUE for check boxes
> that where selected and NULL for the ones leaved clear? The final
table
> with the data stored should look like this:
>
>
+---------+-----------------+-----------+-----------+-----------+-------
> -----+-----------+
> | userid | date | option1 | option2 | option3 | option4 |
> option5 |
>
+---------+-----------------+-----------+-----------+-----------+-------
> -----+-----------+
> | 1 | 2002-06-25 | true | true | false | false
> | true |
> | 2 | 2002-06-25 | false | true | true | false
> | false |
> | 2 | 2002-06-25 | true | false | false | true
> | true |
>
+---------+-----------------+-----------+-----------+-----------+-------
> -----+-----------+
>
> Haw can this be achieved? If no can do, I will have to do if
else if
> else if
else if
and way too far down the page
else.
>
> Thanks in advance,
>
> <mailto:webmaster@icaam.com.ar> Cesar Aracena
> CE / MCSE+I
> Neuquen, Argentina
> +54.299.6356688
> +54.299.4466621
>