mardi 25 avril 2017

Check all checkbox initially and then check and uncheck based on status from database

I want to take attendence of students. So at first I need to load the student names with all checkbox checked. Then if I uncheck one student & submit, then corresponding status becomes 0. So after page reload I want all other students checkbox to be checked and one student remain unchecked.

Here code for load students,

$.ajax({

type: 'POST',
dataType:'json',
url: "<?php echo base_url();?>teacher/Teachers/getstudent",

data: {c_id:c_id},
success: function(result) {

console.log(result);
$("#holidays_events").show();

if(result!='')
  {

  var trHTML = '';
  $('#myTable').html(''); 
  trHTML='<tr><td><b>Name</b></td><td><b>Attendence</b></td></tr>';
  $.each(result, function (i, item) {


  trHTML += '<tr><td>'+item.name+'</td><td><input type="checkbox"  checked="true" class="stid" name="stid[]" value="'+item.id+'" ></td><td><input type="hidden" name="stid1[]" value="'+item.id+'" ></td></tr>';



    });
    $('#myTable').append(trHTML);

    } 


  }

 });

Code after submit

$("#update").click(function(){
$("#submit").hide(); 
$("#update").show();              
alert("Attendence Marked For the Period");
var _data = new FormData($('#formaction')[0]);

$.ajax({

        type: 'POST',
        dataType:"json",
         processData: false,
        contentType: false,
        url: '<?php echo base_url();?>teacher/Teachers/updateStudentAttendence',
        data: _data,
        success: function (result) {
          console.log(result);
          studattendance();
         $("#success_msg").show();
         $("#success_msg").html("Attendence Marked For the Period");

        }

 });

 });

function studattendance()  
{
var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)?  (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);
var currentDate = fullDate.getFullYear()+"-"+ twoDigitMonth +"-"+fullDate.getDate();
var periodid=$("#periodid").val();

 $.ajax({

        type: 'POST',
        dataType:"json",

        url: '<?php echo base_url();?>teacher/Teachers/studattendance',
        data: {currentDate:currentDate,periodid:periodid},
        success: function (result) {
            $.each(result, function (i, item) {
              $("#submit").hide();
              $("#update").show();
            console.log(result[i].student_id);
           $('.stid[value="' + result[i].student_id + '"]').prop('checked', true);
          });
          if(result==''){
            $("#submit").show();
          }
        }

  });


}

studattendance();




Aucun commentaire:

Enregistrer un commentaire