michael1291 Posted July 6, 2011 Share Posted July 6, 2011 Hi i think the title of this thread says it all, does any one know how i can make a check box checked if it was previously checked and the value is in the mysql database, i have did this for radio button and works fine but having great difficulty doing it for check-boxes. Appreciate any help Thanks Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/ Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 How are check boxes harder than radio buttons? Do you have multiple values that could be checked? How are they separated? <?php $boxes = array( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six' ); $values = array(1,2,5); foreach( $boxes as $box => $value ) echo '<label for="box_'.$box.'">'.$value.'</label>' . '<input type="checkbox" id="box_'.$box.'"' . ( in_array($box,$values) ? ' checked="checked"' : '' ) . '><br>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239008 Share on other sites More sharing options...
michael1291 Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks for the reply, yes i have around 50+ check boxes and when they are entered in the database they are separated by a comma. Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239486 Share on other sites More sharing options...
TeNDoLLA Posted July 7, 2011 Share Posted July 7, 2011 So fetch the checkbox data from db and put it in array. You can use explode() to create the array from comma separated string. And then just use the method xyph showed to create the output. Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239492 Share on other sites More sharing options...
michael1291 Posted July 7, 2011 Author Share Posted July 7, 2011 At the minute i have this <?php echo $line["areas"] = 'i' ?' checked="=checked"' :'';?> Obviously it show make the check box checked if the value in areas is = i but instead it checks it for every value other than when the field is blank. What you are saying would work but i don't know how to do that, appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239496 Share on other sites More sharing options...
michael1291 Posted July 7, 2011 Author Share Posted July 7, 2011 I have used explode() and now i have the results in an array but i still cant get it checked when the value is present, it checks it when ever any value is present and when i do == to the value it only checks it when that value and no other is present Sorry for been a pest here Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239509 Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 I'm not sure how your data is stored.. but if($line["areas"] == 'i'){echo ' checked="checked"';} should suffice. no need to have an else there.. Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239512 Share on other sites More sharing options...
michael1291 Posted July 7, 2011 Author Share Posted July 7, 2011 That works great but it only checks the box if the value in the field is == to i therefore when multiple items separated by , are stored it does not check the box . It simply checks the box whenever there is a value there no matter what value it is when i remove and = Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239514 Share on other sites More sharing options...
TeNDoLLA Posted July 7, 2011 Share Posted July 7, 2011 Confusion.. Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239515 Share on other sites More sharing options...
michael1291 Posted July 7, 2011 Author Share Posted July 7, 2011 With the code: if($line["areas"] == 'i'){echo ' checked="checked"';} it only checks the check box if the only value in the field is "i", i need it so that it checks the box if the value "i" is present, no matter what other values are there Sorry for been confusing lol To add to the confusion if i remove the "=" the box becomes checked no matter what value is there. Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239519 Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 $myString = explode(',' $line["areas"]); foreach($myString as $value) { if($value == 'i'){$checkIt = ' checked="checked"';} echo '<option name="'.areas[].'" value="'.areas[].'".$checkIt.' />'; } Quote Link to comment https://forums.phpfreaks.com/topic/241210-make-check-box-checked-if-value-exists-in-my-sql/#findComment-1239546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.