Page 1 of 1

checkboxes + Selected

Posted: Tue Jul 26, 2005 11:31 pm
by Jim_Bo
Hi,

I have 3 checkboxes with the name "car" only 1 of the 3 can be selected, but cant get it to display the correct box that was checked, it defaults to the last checkbox:

Code: Select all

<td><div align="right"><font color="000000">Beginner $4.95</font></div></td>
      <td><input type="radio" name="car" value="blue" <?php echo isset($plan) ? 'checked="checked"' : ''; ?>></td>
    </tr>
    <tr> 
      <td><div align="right"><font color="000000">Standard $7.95</font></div></td>
      <td><input type="radio" name="car" value="red" <?php echo isset($plan) ? 'checked="checked"' : ''; ?>></td>
    </tr>
    <tr> 
      <td><div align="right"><font color="000000">Advanced $10.95</font></div></td>
      <td><input type="radio" name="car" value="orange" <?php echo isset($plan) ? 'checked="checked"' : ''; ?>></td>
How should it be so the correct selected checkbox is checked when $_POST is set?

Cheers

Posted: Wed Jul 27, 2005 12:34 am
by anjanesh
Shouldnt it be :

Code: Select all

<?php echo isset($plan) ? $plan == "a" ? 'checked="checked"' : '' : ''; ?>
..
<?php echo isset($plan) ? $plan == "b" ? 'checked="checked"' : '' : ''; ?>
..
<?php echo isset($plan) ? $plan == "c" ? 'checked="checked"' : '' : ''; ?>
?

Posted: Wed Jul 27, 2005 1:50 am
by Jim_Bo
Hi,

Tried that, doesnt seem to work?

Thanks

Posted: Wed Jul 27, 2005 2:07 am
by anjanesh
Whats the value of $plan anyway when you run your script ?

Posted: Wed Jul 27, 2005 2:11 am
by Jim_Bo
Its in the first post, each check box has a different value ..

Cheers

Posted: Wed Jul 27, 2005 2:22 am
by anjanesh
If its a POST value then replace $plan with $_POST['plan']

Posted: Wed Jul 27, 2005 7:49 pm
by harrisonad
hi, I would use radiobox for this instead of checkbox for it has its own function. But anyway, your problem is already answered in this forum many times now.
when you set the name of the 3 checkbox to 'car' as in...

Code: Select all

<input type=checkbox name=car value=1>Option 1<br>
<input type=checkbox name=car value=2>Option 2<br>
<input type=checkbox name=car value=3>Option 3<br>
you are only overwriting the first two checkboxes with the last one you created, so when the form submits, the value of the last checkbox is the value of $_POST['car'].
The solution is to put brackets, '[]' after the name to create an array of checkboxes.

Code: Select all

<input type=checkbox name='car&#1111;]' value=1>Option 1<br>
<input type=checkbox name='car&#1111;]' value=2>Option 2<br>
<input type=checkbox name='car&#1111;]' value=3>Option 3<br>
So if you print_r() the value of $_POST['car'], it will output...

Code: Select all

// one checkbox selected
Array
{
 &#1111;0] => 1  
}
// all checkboxes selected
Array
{
 &#1111;0] => 1  
 &#1111;1] => 2  
 &#1111;2] => 3  
}
you are now free what to do with this array.

Hope that helps.