Hello all, I'm having a small problem to do with checkboxes. Here is the code : <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>" <?php if($homelink = 1) { echo"checked"; } elseif($homelink !=1) { echo"unchecked"; } ?>/> Code (markup): $homelink can only be 1 or 0 (true or false) and yet when I view this code online the box is checked despite $homelink being set at 0 in the database. Any ideas ? Huge Thanks, Stephen
You are doing assignment, not comparison.... E.G if( $homelink = 1) You are assigning your variable to 1 not comparing the left and right hand side....... For comparison, you shuold use; if($homelink == 1) Code (markup): No. (1 or 0) and (true or false) are technically different things (though may give similar results. <?php $int = 1; $bool = true; if($int == 1) print "int is one\n"; if($int === 1) print "int is int(one)\n"; if($int == true) print "int is true\n"; if($int === true) print "int is boolean true\n"; if ($bool == 1) print "bool is one\n"; if ($bool === 1) print "bool is int(one)\n"; if ($bool == true) print "bool is true\n"; if ($bool === true) print "bool is boolean true\n"; ?> PHP: Output: int is one int is int(one) int is true bool is one bool is true bool is boolean true Code (markup):
Hi, Thanks for your detailed explanation, much appreciated. One issue now, I've changed the code to : <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>" <?php if($homelink == 1) { echo"checked"; } elseif($homelink !==1) { echo"unchecked"; } ?>/> Code (markup): However, I modify $homelink so it's $homelink=1 in the database But still the box remains unchecked, However when i changed $homelink to $homelink=0 in the database The box remains unchecked (what I want) So i'm baffled. Any thoughts ? It's probably me doing something wrong.
Hi Stephen, No problem at all. You dont need the !== on the second comparison (only on 'equals' comparisons) so that is fine as != I have tried your code here and it seems to be fine - I set the homelink value at the top of the code. <?php $homelink = 0; ?> PHP: Output: <input name="homelink" type="checkbox" id="homelink" value="" unchecked/> <?php $homelink = 1; ?> PHP: Output: <input name="homelink" type="checkbox" id="homelink" value="" checked/> After updating the value from the database, you are selecting it out somewhere above the code you pasted I assume - what is the value returned as ?
perhaps "==" for comparing ?? <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>" <?php if($homelink == 1) { echo"checked"; } elseif($homelink !=1) { echo"unchecked"; } ?>/> actually you could try with a negative condition; <input name="homelink" type="checkbox" id="homelink" value="<?php $homelink; ?>" <?php if($homelink != 0) { echo"checked"; } else { echo"unchecked"; } ?>/>
Right, I've finally fixed this. Sadly the solution was alot easier then I expected. the code i posted was part of a function called dropper_admin_page(); in that function i was calling another function containing all the variables, the function name was spelt wrong. Wow i feel embarrased now.....It's amazing how the simplist things are the answer. Although it won't have worked anyway without your help guys, Thank you so so much