0

when user click on a button it displays the dynamic data fetched from the database along with the appropriate check box ..so user has select or check the checkbox of some data and submit the result then those data are stored in database. again that user clicked the same button then again those data are displayed but this time the data which are selected buy the user previously should be checked. I am trying to do that but enable to write code for it in jquery.

below is my code...

javascript

 $(document).ready( function() {    
        $('#location_choose').click(function(e){
                branch();
                document.getElementById('brn_light').style.display='block';

                document.getElementById('fade').style.display='block';

        });

        function branch()
        {               
            //alert(user_id);
            $.ajax({
                        url: "<?php echo base_url(); ?>/login/branch",
                        data: {},
                        type: "post",
                        cache: false,
                        success: function (data)
                        {
                            //alert(data);
                            var obj = $.parseJSON(data);
                            var result = "";
                            <?php foreach($resultsb as $rows){?>


                            for ( var i = 0; i < obj.length; i++ ) 
                            {

                                result+="<table id='branchtable'><tr height='25px' ><td>&nbsp&nbsp</td><td ><input class='test' type='checkbox' name='branch_name' value="+obj[i].id+"<?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>></td><td width='15px'></td><td width='630px'><b>"+obj[i].branch_name+
                                    "</b></td></tr><tr><table id='branch_address' style='background-color:#EBF5FB'><tr><td>&nbsp&nbsp</td></tr><tr><td width='60px'></td><td width='440px'>"+obj[i].address+"</td><td width='40px'></td></tr><td>&nbsp</td></table></tr><tr></tr></table>";

                            }
                            <?php }?>
                            //alert(result);

                            document.getElementById("branch_table").innerHTML=result;
                        },
                        error: function (xhr, ajaxOptions, thrownError) 
                        {
                            //alert(thrownError);
                        }
                });
        }

    });

i tried using

    <?php echo ($rows['branch_id']=="+obj[i].id+"? 'checked' : '');?>

in above code but still it doesnt checked the previous selected one.

Archana Gupta
  • 73
  • 2
  • 9

1 Answers1

0

Give The Same Class of Checkbox and Write Jquery Code

<html>
<head>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script>
$( document ).ready(function() {

                $( '.select_all_text').click(function() {

                         var is_check = $(".select_type").prop('checked');
                                        if(is_check == false)
                                        {
                                            $(".select_type").prop('checked', true);
                                            $(''.select_all_text").text('De-select All');
                                        }
                                        else
                                        {
                                            $(".select_type").prop('checked', false);
                                            $('.select_all_text").text('Select All');
                                        }


                    });


});

</script>
</head>
<body>
<p style="position: sticky; z-index: 100; width: 50%;"><a  id="select_all" onclick="check_uncheck" style="margin-right: 2%;" href="javascript:void(0)">Select all</a> 
 <?php
        foreach($data_list as $data)
              {
              ?>
              <input type="checkbox" name="select_type[]" value="<?php echo $item_id; ?>" class="select_type" id="select_type"/>

            <?php
              }
 ?>
</body>
</html>
Samir Lakhani
  • 685
  • 10
  • 19