0

I want to check the checkBox's value in php. Either selected or not. I am trying in a way;

JQuery

<script type="text/javascript">

    $(document).ready(function(){
        $("#save").click(function(){
            var checkBox = $("#boxV").is(':checked');

            $.ajax({
                url: "http://localhost/testing/test.php",
                type:"POST",
                async:true,
                data:{
                    "done":1,
                    "checkBox" : checkBox
                },
                success: function(data){
                        $('#result').append(data);
                }   
            });
        });
    });



 </script>

PHP

<?php
    if(!isset($_POST['checkBox'])){
        echo ('0');
    }else{
        echo ('1');
    }
    ?>

HTML

 <li><input type="checkbox" id = "boxV" > Check</li><br>

The problem is that both times (Selected , not selected) the result div is filled with 1. Is there any problem?

Amar
  • 855
  • 5
  • 17
  • 36

5 Answers5

1

In PHP, the isset function is testing whether the array contains the element, not whether the value is true. Since you are setting:

data:{
    "done":1,
    "checkBox" : …
}

isset will always return true.

You don’t normally see this behavior in a normal form, since the browser does this differently: if the checkbox is not checked, then the browser will completely leave it out. However, since you are using Ajax, you are implementing your own behaviour of always including it.

In PHP, you can use the following test:

if(isset($_POST['checkBox']) && $_POST['checkBox']) {
    //  checked
}
else {
    //  unchecked
}

Note that I have changed the test to a positive one. If you really want a negative one:

if(!isset($_POST['checkBox']) || !$_POST['checkBox']) {
    //  unchecked
}
else {
    //  checked
}

Alternatively, you can rewrite your JavaScript logic to only include the checkbox if the value is true.

Manngo
  • 14,066
  • 10
  • 88
  • 110
0

You can pass checkBox variable conditionally as describe below:

$(document).ready(function(){
        $("#save").click(function(){
            var checkBox = $("#boxV").is(':checked');
            if(checkBox) {
                var data = {
                   'done': 1,
                   'checkBox': checkBox 
                };
            }else{
                 var data = {
                   'done': 1,                       
                 };
            }
            $.ajax({
                url: "http://localhost/testing/test.php",
                type:"POST",
                async:true,
                data:data,
                success: function(data){
                     $('#result').append(data);
                }   
            });
        });
    });
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
  • why down votes? This is also the solution. Its not compulsory that a problem can be solved with only one solution. – Davinder Kumar May 01 '17 at 12:06
  • @Edwin - I've describe my changes, but FYI down vote means this information is not use full, see here http://stackoverflow.com/help/privileges/vote-down – Ahmed Ginani May 01 '17 at 13:06
0

Your $_POST['checkBox'] is always going to be set, either to 0 or 1. So your PHP should look like this-

    <?php
    if(isset($_POST['checkBox']) && $_POST['checkBox'] === '1'){
        echo ('1');
    }else{
        echo ('0');
    }
    ?>
T.Shah
  • 2,768
  • 1
  • 14
  • 13
0
 $(document).ready(function(){
    $("#save").click(function(){

    var chkVal =  $('input[name$=chkBox]:checked').val();
    var checkBox;
     if(typeof(chkVal) != 'undefined'){
        checkBox = 1;
     }else{
        checkBox = 0;
     }

     $.ajax({
            url: "http://localhost/testing/test.php",
            type:"POST",
            async:true,
            data:{
                "done":1,
                "checkBox" : checkBox
            },
            success: function(data){
                    $('#result').append(data);
            }   
        });
    });
});

PHP Code

<? php
  if($_POST['checkBox'] != 1){
    echo ('0');
  }else{
     echo ('1');
  } 
?>

HTML

<li><input type="checkbox" id = "boxV" name="chkBox" value="1"> Check</li>
PHP Dev
  • 483
  • 3
  • 16
0

isset function checks if variable is set, it does not check the value of variable. If a variable has false(0) value then variable is set because variable has a value. Not matter true or false. You sending "checkBox" in ajax every time so on every call your variable will be set. For this you have to use this.

if($_POST['checkBox']) {
   //true
}

or

if($_POST['checkBox'] === true){
//true
}
Davinder Kumar
  • 652
  • 4
  • 17