1

I am having issues on a jquery function to update the mysql table with a checkbox. No matter what if I check the box or not this function only updates the table with a 0.

Here is my jquery function:

$(function(){
$(document).on('click','.editEntry',function(e){
  //process here you can get id using 
  var entryId = $(this).data('id');
  var activeEntry = $(this).is(':checked') ? 1 : 0;
  $("#id").val( entryId );
  $("#active").val( activeEntry );
    $('#update-entry').click( function (event) {
     $.ajax({
     "dataType": 'json',
     "type": "POST",
     "url": "includes/process-entry.php",
     "data":{
     "id" : $('#id').val(),
     "active" :  $('#active').val()
     },
     "success": function() {
     $('#updated').show();
     window.setTimeout(function(){location.reload()},400);
     }
    });
  });
});
});
<input name="id" type="text" disabled="disabled" id="id" size="4" value ='1'>
<input type="checkbox" name="active" id="active">

Can anyone let me know what is incorrect on why it is not inserting the '1' into the database when it is checked?

Thank you in advance. Let me know if there is more information

2 Answers 2

1

Change

"active" :  $('#active').val()

to

"active" : activeEntry

Checkboxes don't have a value attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

agree, you can check if checkbox is (or not) checked via .is(':checked')
0

I have figured out how to get it to work properly:

Had to move this line:

  var activeEntry = $(this).is(':checked') ? 1 : 0;

inside the #update-entry function. Here is the updated code:

$(function(){
$(document).on('click','.editEntry',function(e){
  //process here you can get id using 
  var entryId = $(this).data('id');
  $("#id").val( entryId );
  $("#active").val( activeEntry );
    $('#update-entry').click( function (event) {
     var activeEntry = $(this).is(':checked') ? 1 : 0;
     $.ajax({
     "dataType": 'json',
     "type": "POST",
     "url": "includes/process-entry.php",
     "data":{
     "id" : $('#id').val(),
     "active" :  $('#active').val()
     },
     "success": function() {
     $('#updated').show();
     window.setTimeout(function(){location.reload()},400);
     }
    });
  });
});
});
<input name="id" type="text" disabled="disabled" id="id" size="4" value ='1'>
<input type="checkbox" name="active" id="active">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.