0

I have checkboxes that are generated dynamically with value from database in a while statement as such :

 while($getplands = mysqli_fetch_assoc($getplanresults)){
     <tr>
<td><input class="thisinput" type="checkbox" value="'.$getplands['id'].'"></td>
     </tr>
}

When the user clicks on a checkbox two things must happen

  1. data is sent to ajax and callback response displayed in a div

  2. the checkbox is checked

If I'm using below code, the checkbox does not check but I'm able to fetch data from database with ajax and display user choice.

$('.thisinput').click(function(e){//a checkbox is checked

$(this).prop('checked', true);//does not work here

 $.ajax({//works here
                        type: 'POST',
                        url: 'fetch.php',
                        data:values,
                        dataType: 'json',
                        success: function(datas)
                        {
                        $('#mychoice > tbody:last').append(datas.mychoice);
                        }

                 });    
});

If I'm using below code the checkbox is checked but I'm unable to fetch data from database with ajax and display user choice.

    $('.thisinput').on('change', 'input:checkbox', function(){
    $(this).prop('checked', true);//works here

 $.ajax({//does not work here
                        type: 'POST',
                        url: 'fetch.php',
                        data:values,
                        dataType: 'json',
                        success: function(datas)
                        {
                        $('#mychoice > tbody:last').append(datas.mychoice);
                        }

                 });   
    });
Sadie
  • 163
  • 10

1 Answers1

1

Alright so I just fixed the problem. Below code works for me.

  $('.thisinput').on('change',function(){
    $(this).prop('checked', true);//works here

 $.ajax({//works here
                        type: 'POST',
                        url: 'fetch.php',
                        data:values,
                        dataType: 'json',
                        success: function(datas)
                        {
                        $('#mychoice > tbody:last').append(datas.mychoice);
                        }

                 });   
    });
Sadie
  • 163
  • 10