0

i try to add checked in my checkbox if value in checkbox is same with value from database :

My database :
Column = skin_type 
Value = Normal, Dry, Oily

My Default html checkbox :
<input type="checkbox" name="skin_type[]" value="Normal">Normal &nbsp;
<input type="checkbox" name="skin_type[]" value="Dry">Dry &nbsp;
<input type="checkbox" name="skin_type[]" value="Oily">Oily &nbsp;
<input type="checkbox" name="skin_type[]" value="Combination">Combination &nbsp;
<input type="checkbox" name="skin_type[]" value="Sensitive">Sensitive

so if value is Normal, Dry, Oily checkbox with this value will checked , example like below

enter image description here

Below is my current php script (i try to explode the data first but not sure how to add checked if value is equal with value from database) :

<?php 
$query_skin_type = mysql_query("SELECT skin_type FROM `customers` WHERE customers_id='".$_GET['cID']."'");
while($info = mysql_fetch_array($query_skin_type)) {

$skin_type = explode(", ", $info['skin_type']);

    foreach ($skin_type as $value) {
    $value.'<br>';
    }
}
?>
5
  • How you are storing this value in db table? Commented Oct 9, 2012 at 5:17
  • @LearneR -> if($_POST['skin_type'] == TRUE) { $skin_type_out = implode(', ', $_POST['skin_type']); } Commented Oct 9, 2012 at 5:21
  • Skin type is listing i mean Normal,Dry,Oily,Combination,Sensitive are static or dynamic? Commented Oct 9, 2012 at 5:23
  • static , i store all value in one column, if this below than 3 input i can seperate and create each value with different column(simple method) but i have more than 10 value for checkbox. Commented Oct 9, 2012 at 5:25
  • OP, give my answer a try. It should work for what you're trying to accomplish. Commented Oct 9, 2012 at 5:31

4 Answers 4

1

You can just write a little function to check against your $skin_type array. For example:

$skin_type = explode(", ", $info['skin_type']);

function isChecked($val, $arr){
  if(in_array($val, $arr)){
    echo 'checked';
  }
}

<input type="checkbox" name="skin_type[]" value="Normal" checked="<?php isChecked('Normal', $skin_type); ?>">Normal &nbsp;
//etc...

Please note that I would go about generating the markup in an entirely different way, but this will suffice without any refactoring.

Sign up to request clarification or add additional context in comments.

3 Comments

not sure why , its checked all my checkbox. Btw i already write my solution using foreach inside input(this from your idea).
You need to change the first argument given to isChecked() to match the input value for each checkbox.
okay, i think we have 2 solution here but better use your code @Matthew it simple. Thanks
0
<input type="checkbox" name="skin_type[]" value="Normal" 
<?php
foreach ($skin_type as $value) {
if($value == "Normal") { echo "checked='checked'"; }
}
?> >Normal &nbsp;

<input type="checkbox" name="skin_type[]" value="Dry" <?php
foreach ($skin_type as $value) {
if($value == "Dry") { echo "checked='checked'"; }
}
?> >Dry &nbsp;

<input type="checkbox" name="skin_type[]" value="Oily"  
<?php
foreach ($skin_type as $value) {
if($value == "Oily") { echo "checked='checked'"; }
}
?> 
>Oily &nbsp;

<input type="checkbox" name="skin_type[]" value="Combination"  
<?php
foreach ($skin_type as $value) {
if($value == "Combination") { echo "checked='checked'"; }
}
?> 
>Combination &nbsp;

<input type="checkbox" name="skin_type[]" value="Sensitive"  <?php
foreach ($skin_type as $value) {
if($value == "Sensitive") { echo "checked='checked'"; }
}
?> 
>Sensitive

1 Comment

I won't downvote, but you don't need to loop at all for this and you've written way too much code for the task at hand...
0
$skin_type = explode(", ", $info['skin_type']);


<input type="checkbox" name="skin_type[]" value="Normal" <?php if(in_array("Normal",$skin_type)){ ?>?checked="checked" <?php } ?>>Normal &nbsp;

Comments

0

check box checked depend on data base value

'<input type="checkbox" class="checkbox" name="s_info[]" id="'.$ccid.'-S-'.$value['receipt_no'].'" value="'.$ccid.'-S-'.$value['receipt_no'].'" '. ($value['verified']? "checked":"") .'/>'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.