Amar
Amar

Reputation: 905

Checking value of check box using php

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?

Upvotes: 0

Views: 862

Answers (5)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

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);
                }   
            });
        });
    });

Upvotes: 0

Davinder Kumar
Davinder Kumar

Reputation: 662

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
}

Upvotes: 0

PHP Dev
PHP Dev

Reputation: 503

 $(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>

Upvotes: 0

Manngo
Manngo

Reputation: 16443

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.

Upvotes: 1

T.Shah
T.Shah

Reputation: 2768

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');
    }
    ?>

Upvotes: 0

Related Questions