lundi 24 février 2020

inserting checkbox value to database

i have 2 checkboxes whose values i am sending from ajax to controller to post the data(approved/reject) in database now i want that if both the checkboxes are unchecked pending should be inserted in database and also if i uncheck the checked box the value should be updated again to pending.what can be the logic

html

<td> <input type="checkbox" id="APPROVED_" class="approve_chk" name="chkBestSeller" value="APPROVED"  data-id=""></td>
<td> <input type="checkbox" id="REJECTED_" class="reject_chk"  name="chkBestSeller" value="REJECTED" data-id=""> </td>

Jquery

     $('.approve_chk').on('change', function (e) {
    var pswd = prompt("enter password to confirm");
    if (pswd == 'approve') {

        alert('APPROVED');

        var currentEle = $(this).attr('id');
        var chk = currentEle.split("_");
        console.log(chk[0]);
        var status=chk[0];
        var ID=chk[1];

        e.preventDefault();
        $.ajax({
            url:'EmployeeChkBoxStore',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type:'POST',
            dataType:'json',
            data:{status:status,ID:ID},
            success:function(data){
                console.log(data);
                alert('success');
            }
        });



    } else {
        alert('NOT APPROVED');
        $(this).prop('checked', false);
    }

});






     $('.reject_chk').on('change', function (e) {
    var pswd = prompt("enter password to confirm");
    if (pswd == 'reject') {
        alert('REJECTED');
        $(this).closest('tr').find('.approve_chk').prop('checked', false);

        var currentEle = $(this).attr('id');
        var chk = currentEle.split("_");
        console.log(chk[0]);
        var status=chk[0];
        var ID=chk[1];

        e.preventDefault();
        $.ajax({
            url:"EmployeeChkBoxStore",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type:'POST',
            dataType:'json',
            data:{status:status,ID:ID},
            success:function(data){
                console.log(data);
                alert('success');
            }
        });

    } else {
        alert('NOT REJECTED');
        $(this).prop('checked', false);
    }
});

controller

    public function EmployeeChkBoxStore(Request $request){
    $data=new Leave();
    $data->status=$request->get('status');
    $data->id=$request->get('ID');
     DB::update("UPDATE `leaves` SET status = '$data->status' WHERE id = '$data->id'");
     }



Aucun commentaire:

Enregistrer un commentaire