checkboxes + Selected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

checkboxes + Selected

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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"' : '' : ''; ?>
?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Tried that, doesnt seem to work?

Thanks
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Whats the value of $plan anyway when you run your script ?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Its in the first post, each check box has a different value ..

Cheers
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

If its a POST value then replace $plan with $_POST['plan']
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
Post Reply