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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 =

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.