MOHAMMED MOHAMMED - 4 months ago 12
PHP Question

display if checkbox is checked while editing

As i had gone through several solutions i didnt get an solution for this. when i go for editing and open the page i want to show the list of check boxes as marked that i chooses while inserting.
Here is my view page..

<div class="form-group">
<label class="control-label col-sm-1" for="courses">Courses Completed</label>
<div class=" col-sm-5">

<input type="checkbox" name="select_course[]" id="select_course" value="10" <?php if($row->courses =='10') echo "checked" ;?>>10
<input type="checkbox" name="select_course[]" id="select_course" value="12" <?php if($row->courses =='12') echo "checked" ;?>>12
<input type="checkbox" name="select_course[]" id="select_course" value="degree" <?php if($row->courses=='degree') echo "checked" ;?>>degree

</div>
</div>


by doing like this none of the checkbox is showing as checked state.the value inserted is like an array and my table looks like this

id name address sex courses places image password
1 nesru v male "10" tamilnadu upld-file1469095130.jpg l
3 siraj koonathil house male "10,12" tamilnadu upld-file1469167954.jpg d

Answer

As per table you have posted it seems you are storing comma separated value in courses, for that you need to convert in array and check using in_array() function

<div class="form-group">
    <label class="control-label col-sm-1" for="courses">Courses Completed</label>
   <?php $idArray = explode(",",$row->courses); ?>
    <div class=" col-sm-5">
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="10" <?php if(in_array('10',$idArray)) echo "checked" ;?>>10
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="12" <?php if(in_array('12',$idArray)) echo "checked" ;?>>12
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="degree" <?php if(in_array('degree',$idArray)) echo "checked" ;?>>degree
    </div>
</div>
Comments