Jump to content

Make check box checked if value exists in my sql


michael1291

Recommended Posts

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

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>';

?>

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

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

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 =

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.