0

jquery:

$("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;
     }
     alert(flagvalue);
     $.ajax({
         url: "../php/insert.php",
         method: "get",
         data: {"flagvalue":flagvalue,"id":id},
         dataType: "text"  
         success:function(data)  
         {  
            $('#ohmygod').html(data); //it doesnt echo anything from here :(
         }
     });
});

php:

$insertMessage = "";
$bendera = filter_input(INPUT_POST, 'flagvalue', FILTER_SANITIZE_SPECIAL_CHARS);
$id = $_POST['id'];
if(!empty($bendera)) { //also doesn't work if change it to isset
    $insertMessage = "Reached the php part";
    $updateflag = "UPDATE sintok SET flag='$bendera' WHERE id='$id'"; 
    mysqli_query($connection, $updateflag); 
}
echo $insertMessage;

html:

<div id="ohmygod"></div>

include another php into html file:

<input value='.$row['id'].' type=checkbox name=flag id=flag '.$tick.'>

I have one checkbox which consist of two values, 0(unchecked) and 1(checked). From above code I don't see any changes except for the background-color of the row once the user has checked the checkbox button. But it didnt update the database. My question is, what is the correct syntax to update the data instantly from checkbox selection.

27
  • flagvalue != flag so $bendera is probably empty. You also are open to SQL injections. Use parameterized queries. Also if 0 is a valid value for $bendera to be you can't use empty, use isset. Commented Nov 4, 2016 at 23:51
  • I've already change the flagvalue. How to use parameterized queries? Commented Nov 4, 2016 at 23:54
  • See php.net/manual/en/mysqli.quickstart.prepared-statements.php. Did you correct the empty check? Commented Nov 4, 2016 at 23:55
  • Okay, then that should execute as is (I think). Not working still? The parameterized queries are to stop the potential SQL injection. Commented Nov 4, 2016 at 23:57
  • Is your db column id is integer ? Commented Nov 4, 2016 at 23:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.