Checkbox, POST, and array's

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Checkbox, POST, and array's

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Checkbox, POST, and array's

Post by AbraCadaver »

Check the quotes around $db01 and $db02 in the $box_array.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
shridhar.govindaiah
Forum Newbie
Posts: 3
Joined: Wed Jun 30, 2010 8:13 am

Re: Checkbox, POST, and array's

Post 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,
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Checkbox, POST, and array's

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Checkbox, POST, and array's

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Checkbox, POST, and array's

Post by IGGt »

Cheers for that, semi-colon removed, and it works great. Quite obvious when you know how.


cheers
Post Reply