3

I want to set value for the check when it is unchecked. But every time I post my data whatever it is checked or unchecked, the value will be '1'

I tried to set !isset($check) in post.php, but the value also will be 1.

How can I set the different value when I uncheck the checkbox.

<input type="checkbox" id="check" name="check" value="1">

The jquery code is below:

var check = $('#check').val();
var Data = {check:check};
$.ajax({
    type:"POST",
    url:"post.php",
    data:Data,
    beforeSend: function() {
        $("#btn-submit").html('sending');
    },
    success: function(data) {
    }
});
2
  • :checked selector. prop('checked') or other ways. Internet is full of tutorials. Commented Jul 17, 2017 at 6:57
  • $('#check').val(); change to $('#check').is(":checked"); will send true or false depending on state Commented Jul 17, 2017 at 6:58

4 Answers 4

3

You should use is(':checked') to determine whether the checkbox is checked or not:

$("#check").on('change', function() {
  var checkFlag = 'unchecked';
  if ($(this).is(':checked')) {
    checkFlag = 'checked';
  }

  console.log(checkFlag);

  $.ajax({
    type: "POST",
    url: "post.php",
    data: {
      check: checkFlag
    },
    beforeSend: function() {
      $("#btn-submit").html('sending');
    },
    success: function(data) {}
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="check" name="check" value="1">

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

2 Comments

I don't see how this helps the OP send the value in his AJAX request?
Well, it gets you to know whether its checked or not and he just needs to send that as a parameter.
1

The issue is because the val() of the checkbox is always the same regardless of whether the element is checked or not.

To do what you require you can set the value based on the checked state. Try this:

var $check = $('#check');

$.ajax({
  type: "POST",
  url: "post.php",
  data: { 
    check: $check.prop('checked') ? $check.val() : 0 
  },
  beforeSend: function() {
    $("#btn-submit").html('sending');
  },
  success: function(data) {
    // do something with the response
  }
});

Comments

0

Try the following: var check = $('#check').is(":checked");

When checkbox is checked

You will get true

When checkbox is not checked

You will get false

Comments

0
if ($("#check").is(':checked')) {
   var check = $('#check').val();
}else{
   var check = 0;
}
var Data = {check:check};
$.ajax({
    type:"POST",
    url:"post.php",
    data:Data,
    beforeSend: function() {
        $("#btn-submit").html('sending');
    },
    success: function(data) {
    }
});

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.