Reputation:
I'm writing a system and have a checkbox in a form.
My problem is, when i press the edit button, the checkbox cannot show me the tick in the checkbox, I have tried to change checkbox type to text, it can show me the value. I have set the value if value = 1 is tick, if value = 0 is no tick. How can shows up tick in the form? Anyone can guide me to solve it?
Below is my coding:
Checkbox
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Pricing<span style="color:red;"> </span></label>
<div class="col-lg-8">
<input type="text" name="rm_option" id="rm_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?> ><strong> RM </strong></input>
<input type="text" name="point_option" id="point_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Full Point </strong></input>
<input type="text" name="partial_option" id="partial_option" value="1" <?php if(_POST[$value]=='1'){echo "checked='checked'";} ?>><strong> Partial Point + RM </strong></input>
</div>
</div>
Checkbox function
<?php
$sql = "select * from promotion_list where id=" . $_GET['id'];
$arr_sql = db_conn_select($sql);
foreach ($arr_sql as $rs_sql) {
foreach ($rs_sql as $key => $value) {
?>
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php
}
?>
When I press the edit button, other column can show up in the form, only the checkbox cannot show me the tick. Below is the output picture:
If I change Checkbox type to text, below is the output (Prove inside the checkbox got value)
I have stuck in this problem already 1 week, hope someone coding hero can guide me to solve this problem. Thanks a lot.
Upvotes: 0
Views: 134
Reputation: 1052
Add below code exactly after this line:
$("#<?php echo $key ?>").val("<?php echo $value?>");
it became:
$("#<?php echo $key ?>").val("<?php echo $value?>");
<?php if($value == 1){ ?>
$("#<?php echo $key ?>").attr("checked", true).prop("checked", true);
<?php } ?>
its clear that you are using jquery to work on the checkbox.
Upvotes: 0