user_777
user_777

Reputation: 805

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

Upvotes: 0

Views: 893

Answers (3)

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

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>

Upvotes: 2

Hardik Patel
Hardik Patel

Reputation: 716

You should put following code :

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

Upvotes: 0

Danimal
Danimal

Reputation: 333

The attribute for checked state is checked=""

Put your php code in the quotes

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

Upvotes: 0

Related Questions