-2

I have form containing multiple checkboxes and I want that checkboxes to be checked according to their values from database using jquery after clicking edit button? Thanks in advance..

Here is view:

<form id="frmAddBand" name="frmAddBand" method="post" action="" onsubmit="return (checkc() == 1)">

            <fieldset>

                <ol>
                     <li>
                        <label >Band Code <em>*</em></label>
                        <input id="bandCode" type="text" name="bandCode"  />
                        <span id="code_status" style="color:red;" ></span>  
                  </li>
                  <li>
                        <label >Band Name<em>*</em></label>
                        <input id="bandName" type="text" name="bandName" />

                  </li>
                  <li>
                        <label >Designation<em>*</em></label><br/><br/><div style="height:100px;background-color:#fff;overflow:auto;"><br/><?php $des_code = array_keys($designation);
                              $des_name = array_values($designation);
                             for($i=0;$i<count($des_code);$i++){?>
                        <input type="checkbox"  name="des" id="des" value="<?php echo $des_code[$i];?>"/><span style="font-size:12px;"><?php echo $des_name[$i];?></span><br/><br/><?php }?>
                        </div>
                  </li>

                                            <li class="required new">
                                            <input type="text" id="demo" name="demo" />
                        <em>*</em> Required field                        </li>
                                        </ol>    


                                    <p><input id="click_submit" name="click_submit" Value="Save" type="button">
                   <input id="click_cancel" name="click_cancel" Value="Cancel"  type="button" >                 
                </p>

            </fieldset>
        </form>

controller:

public function band_edit($id)
            {
            $this->load->helper('url');
            $this->load->database();
            $this->load->model('Ohs_Bandmodel');


            $data = $this->Ohs_Bandmodel->get_by_id($id );
                echo json_encode($data);
            }

script:

function edit_band(id)
                    {

                        $("#frmAddBand")[0].reset();

                        //Ajax Load data from ajax
                     $.ajax({
                            url : "<?php echo base_url(); ?>index.php/Ohs_Cband/band_edit/" + id,
                            type: "GET",
                            dataType: "JSON",
                            success: function(data)
                            {
                     var b=data.desig;
                     var arr = b.split(",");
                    //alert(arr[1]);
                                for(i=0;i<arr.length;i++){
                                    $('input[type=checkbox]').each(function(){
                                        //alert(arr[i]);
                                        if($(this).val()==arr[i]){
                                $(this).prop('checked', true);
                                }
                                else{
                                    $(this).prop('checked', false);
                                }
                                })
                                }
                                $('[name="bandCode"]').val(data.band_code);
                                $('[name="bandName"]').val(data.band_name);

                                $('[name="click_submit"]').val('Update');
                                $('[name="bandCode"]').attr("readonly", true);
                                $('[name="bandCode"]').css('background-color' , '#DEDEDE');
                                //$("#click_cancel").attr('visibility', 'visible');  
                                $('#click_cancel').show();
                            },
                            error: function ()
                            {
                                //alert('Error get data from ajax');
                                $('[name="bandCode"]').attr("readonly", false);
                                $('[name="bandCode"]').css('background-color' , '#FFF');
                                $('[name="bandCode"]').removeAttr("disabled"); 
                                $("#Notify").data("mtype","E");
                                $("#Notify").data("msg","error in record.")
                                $('#Notify').trigger('click');
                                //$("#click_cancel").attr('visibility', 'hidden');   
                            }
                        });
                    }

this is my form

when I click on edit record all works fine but instead of checking 2 checkbox based on data in records it checks only 1 checkbox.Any help would be appreciated.Thanks

5
  • paste some code Commented Jan 5, 2017 at 12:21
  • You need to add your code in for both the form and the php you have already have. What have you tried yourself and what issues did you run into. Your question is very plain and needs some editing. Commented Jan 5, 2017 at 12:23
  • Please format your code in your question, it can be done with hightlighting code and CTRL+K, thank you! Commented Jan 5, 2017 at 12:29
  • i have values of checkboxes in array(data) . i want the checkbox to be checked based on those values when i click edit button.. Commented Jan 5, 2017 at 12:36
  • Possible duplicate of Update form using Ajax, PHP, MYSQL Commented Jan 5, 2017 at 13:05

1 Answer 1

0

To check/uncheck a checkbox in jQuery :

1 - To check

$("yourCheckboxId").prop("checked",true);

2 - To uncheck

$("yourCheckboxId").prop("checked",false);

Just use this sample of code in your logic

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

2 Comments

No basically i have records which i want to update so when i click on edit records all the data should be there in the html form. So i want the checkboxes to be checked according to their value in form on edit click
I have array of records so using jquery i want to check only those checkbox whose value is in the array..

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.