Forum Moderators: coopster

Message Too Old, No Replies

Having troubles with checkboxes and PHP

         

fib_81

1:20 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Hi Everyone,

I am having problems with checkboxes.

I have there rows of checkboxes and each row has seven checkboxes.

while($count < 3)
{
echo '<table cellpadding=0 cellspacing=0><tr><td>';
echo '<input type=checkbox name=check_sun STYLE="font-size:10px;" value=1> </font></td>';
echo'<td width=20>
<input type=checkbox name="check_mon[]" STYLE="font-size:10px;" value=2></td>';

echo'<td width=20>
<input type=checkbox name="check_tues[]" STYLE="font-size:10px;" value=3></td>';

echo'<td width=20>
<input type=checkbox name="check_wed[]" STYLE="font-size:10px;" value=4></td>';

echo'<td width=20>
<input type=checkbox name="check_thurs[]" STYLE="font-size:10px;" value=5></td>';

echo'<td width=20>
<input type=checkbox name="check_fri[]" STYLE="font-size:10px;" value=6></td>';

echo'<td width=20>
<input type=checkbox name="check_sat[]" STYLE="font-size:10px;" value=7></td>';

echo'</tr>';

echo '</tr>';
echo '<tr>';
echo '<td><br></td>';
echo '</tr></table>';

$count++;

}

What I want to do is record the values for checked and unchecked checkbox for each row. For example, if in the first row of checkboxex, only the the first three checkboxes are checked the database will record "1" for those first three checkboxes and record "0" for the last four checkboxes. I want to do this for every row of checkboxes. However, when I do, the database is not record the correct checked checkbox for each row. For example, if I checked the first three checkboxes on the first and leave the rest of the checkboxes in the first row unchecked and then checked the the last four checkboxes in the second row, the database would record "1" for the first row of checkboxes, which is not true.

"1"= true for checked checkbox
"0"= not checked checkbox

Rows and columns of CheckBoxes that are checked
-------------------------------------
1 1 1 0 0 0 0
--------------
0 0 0 1 1 1 1
--------------
0 0 0 0 0 0 0

The result in the database would be:
---------------
1 1 1 1 1 1 1
---------------
0 0 0 0 0 0 0
---------------
0 0 0 0 0 0 0

when the correct insertation in the database should be:
---------------
1 1 1 0 0 0 0
---------------
0 0 0 1 1 1 1
---------------
0 0 0 0 0 0 0

Can someone help? I hope I was clear on my problem.

Thanks,
Fib_81

ironik

2:33 am on Mar 22, 2005 (gmt 0)

10+ Year Member



PHP only finds the checkbox if it is checked, otherwise it doesn't exist. You have to check that it exists to see whether it has been checked:

if (isset($_POST['check_sat'][0]))
{
// saturday checkbox has been checked
} else {
// saturday checkbox has NOT been checked
}

fib_81

4:58 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Hi Ironik,

I think you told me that before. Sorry for having you repeat the instructions all over again. So...I cannot assign the first sunday checkbox in the first row and the second sunday in the second row as sunday[0] and sunday[1] with respect. I only can do that if both first and second sunday is checked? Thanks for your patience.

Regards,
Fib_81

ironik

9:03 pm on Mar 22, 2005 (gmt 0)

10+ Year Member



Yep, spot on. The first sunday checkbox will not exist if it isn't checked, therefore the second sunday will be called sunday[0]. Your best bet is to explicitely name the checkboxes, then do the testing on the explicit names... e.g:

<input type=checkbox name="check_mon_1" STYLE="font-size:10px;" value=1>
<input type=checkbox name="check_mon_2" STYLE="font-size:10px;" value=1>
<input type=checkbox name="check_mon_3" STYLE="font-size:10px;" value=1>

Then your PHP:

$num_checkboxes = 3;
for ($i = 1; $i <= $num_checkboxes; $i++)
{
if (isset($_POST['check_mon_' . $i]))
{
// Apply action here
}
}

Hope that makes sense