Page 1 of 1

Checkbox, POST, and array's

Posted: Mon Aug 02, 2010 10:31 am
by IGGt
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:

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/>
This worked fine. However I have now changed it so I have:

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>"; }
?>
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?

Re: Checkbox, POST, and array's

Posted: Mon Aug 02, 2010 10:48 am
by AbraCadaver
Check the quotes around $db01 and $db02 in the $box_array.

Re: Checkbox, POST, and array's

Posted: Tue Aug 03, 2010 4:29 am
by shridhar.govindaiah
Hi IGGT,

Its better if u use double quote in if condition,
or use $db01 as boolean variable, i.e if($db01) or if($db02).

Its not a good practice to compare boolean value with boolean value, I mean false == true. in such case, it always passes if condition.

If the above solution is not working for u, then use "===" in if statement, cos it ll not allow php automatic datatype casting.


Cheers,

Re: Checkbox, POST, and array's

Posted: Tue Aug 03, 2010 11:28 am
by IGGt
cheers, but I'm not quite sure I see what you're saying.

I tried the double quotes:

Code: Select all

$box_array[] = array('name' => 'db01', 'dbname' => 'MySQL01_5083', 'var' => "$db01");
. . .
if  ($box_array[$d]['var'] == "1"); {
I also tried using === inthe IF statement, but it still doesn't work.

I'm sure I must be missing something obvious.

cheers

Re: Checkbox, POST, and array's

Posted: Tue Aug 03, 2010 12:19 pm
by AbraCadaver
OK, that took me a while to spot. You have a ; closing the if before the {. That terminates the if block and the following line is executed regardless of the if condition.

Re: Checkbox, POST, and array's

Posted: Wed Aug 04, 2010 2:25 am
by IGGt
Cheers for that, semi-colon removed, and it works great. Quite obvious when you know how.


cheers