robins robins - 1 month ago 5
PHP Question

Checkbox not selected based on database value

I have 3 checkboxes.These values are saved in db with comma like 1,2,3(select three).In edit i want to select the three checkboxes.But its not done

view

<label class="control-label"> Select Group:</label> </span>
<div class="controls">
<span class="details">
<input type="checkbox" name="group[]" value="1" <?php if(in_array('1', explode(",",$students_list->group_list))) echo( 'selected = "selected"'); ?> />one
<input type="checkbox" name="group[]" value="2" <?php if(in_array('2', explode(",",$students_list->group_list))) echo( 'selected = "selected"'); ?>/>two
<input type="checkbox" name="group[]" value="3" <?php if(in_array('3', explode(",",$students_list->group_list))) echo( 'selected = "selected"'); ?>/>three
<?php echo form_error('group'); ?>
</span>
</div>


$students_list->group_list value like 1,2,3

Anyone please help me

Answer

Change this line:

echo( 'selected = "selected"');

to

echo( 'checked = "checked"');

and try again.

Comments