vendredi 4 novembre 2016

Update the db instantly with checkbox selection using php and jquery

I have two options for the checkbox which are checked(1) and unchecked(0). When user checked the checkbox, it will change the background-color of a tr. But it doesn't update the database. My question is, how to instantly update the checkbox selection?

This is my jquery file:

$("#alllisting input[name='flag']").on('change', function() {
         event.preventDefault();
         var tablerow = $(this).closest('tr');
         var id = $(this).attr('id');
         var flagvalue;

     if($(this).prop('checked') == true) {
         tablerow.css({'background-color':'rgba(175,0,0,0.2)'});
         flagvalue = '1';
     }
     else {
         tablerow.css({'background-color':'rgba(175,0,0,0)'});
         flagvalue = '0';
     }
     $.ajax({
         data: {"id":id,"flagvalue":flagvalue},
         url: "update.php",
         method: "post",
         dataType: "text",
         success:function(answer)  
         {  
             alert(answer); 
         }  
     });
});

and this is my php file

<?php
include 'config.php';
$message = "";

    $bendera = filter_input(INPUT_POST, 'flagvalue', FILTER_SANITIZE_SPECIAL_CHARS);
    $id = filter_input(INPUT_POST, 'id');
    if(!empty($bendera)) {
        alert("Reached the php part");
        $updateflag = "UPDATE hijueputa SET flag='$bendera' WHERE id='$id'"; 
        mysqli_query($connection, $updateflag); 
    }

    mysqli_close($connection);
    echo $message;




Aucun commentaire:

Enregistrer un commentaire