Checkbox, POST, and array's
Posted: Mon Aug 02, 2010 10:31 am
I have a form that includes a set of tick boxes, when you click submit, it reloads the same page, and carries over which boxes are ticked using POST. This worked fine originally, but as the number of tick boxes grew, I have turned the list into an array, and use a FOR loop to recreate the tick boxes, but now it isn't carrying over properly, they are always either All ticked, or all unticked. My original code looked like:
This worked fine. However I have now changed it so I have:
but this returns all boxes ticked. The thing is when I added in a few lines to echo the $db0x variables, they all appear to be correct (some set to 1 some set to 0), but somehow within the FOR loop it thinks they are all set to 1, and so they all get ticked. Is there something happening in here that would cause all of the $db0x variables to get set to 1?
Code: Select all
<?php
if (isset($_POST['db01'])) {
$db01 = 1;
$connections_array[] = array('server' => 'localhost:3306',
'user' => $u,
'database' => 'MySQL01_5083' );
} else { $db01 = 0; };
if (isset($_POST['db02'])) {
$db02 = 1;
$connections_array[] = array('server' => 'localhost:3307',
'user' => $u,
'database' => 'MySQL02_5083' );
} else { $db02 = 0; };
. . .
?>
<body>
. . .
<INPUT TYPE="checkbox" NAME="db01" VALUE="db01"
<?php if ($db01 == 1) {
echo "CHECKED";
} ?>
> MySQL01_5083 <br/>
<INPUT TYPE="checkbox" NAME="db02" VALUE="db02"
<?php if ($db02 == 1) {
echo "CHECKED";
} ?>
> MySQL02_5083 <br/>
Code: Select all
<?php
if (isset($_POST['db01'])) {
$db01 = 1;
$connections_array[] = array('server' => 'localhost:3306',
'user' => $u,
'database' => 'MySQL01_5083' );
} else { $db01 = 0; };
if (isset($_POST['db02'])) {
$db02 = 1;
$connections_array[] = array('server' => 'localhost:3307',
'user' => $u,
'database' => 'MySQL02_5083' );
} else { $db02 = 0; };
. . .
$box_array[] = array('name' => 'db01', 'dbname' => 'MySQL01_5083', 'var' => '$db01');
$box_array[] = array('name' => 'db02', 'dbname' => 'MySQL02_5083', 'var' => '$db02');
. . .
?>
<body>
. . .
for($d = 0; $d <sizeof($box_array); $d++) {
echo "<INPUT TYPE=\"checkbox\" NAME=\"";
echo $box_array[$d]['name'];
echo "\" VALUE=\"";
echo $box_array[$d]['name'];
echo "\" ";
if ($box_array[$d]['var'] == 1); {
echo "CHECKED"; }
echo "> ";
echo $box_array[$d]['dbname'];
echo "</br>"; }
?>