2

I have a checkbox in my HTML file:

<input type="checkbox" id="kopia" name="kopia" class="form-input" value="0">

I'm trying to pass to the PHP file (via AJAX) a couple of values, one of them is a boolean value created by a checkbox (simplified):

name: "kopia"
value: document.getElementById("kopia").checked

If I check via console, it returns true if checked and false if unchecked.

document.getElementById("kopia").checked

If I check via PHP file, it behaves the same:

echo $_POST['kopia']

But when I write something like this:

$checkbox = $_POST['kopia'];
if ($checkbox){
    echo "hey";
} else {echo "bye";}

It always returns "hey", no matter if the checkbox was checked or not. I don't get it. Even if I make something like this:

$checkbox = $_POST['kopia'];
if ($checkbox){
    echo $checkbox;
} else {echo "bye";}

It never returns "bye", no matter if the box was checked or not, but returns true if checked and false if unchecked. This thing has already stolen a couple of hours and that's a couple of hours too much. Any idea, hint?

9
  • 3
    I assume you omitted some JS that changes the value. $_POST values will always be strings in PHP so your if is expecting boolean. Try if ($checkbox == "true") Or use 0 and 1. Commented Mar 3, 2017 at 20:56
  • if ($checkbox == "true") that's a string equaling "true"; boolean's if ($checkbox == true) @AbraCadaver two different animals ;-) Commented Mar 3, 2017 at 20:57
  • 1
    @Fred-ii- Re-read my comment. You won't get boolean values in $_POST vars. That's why when OP echo $checkbox; they get "true" or "false" Commented Mar 3, 2017 at 20:58
  • can you show your ajax code Commented Mar 3, 2017 at 21:01
  • 1
    @Fred-ii- I think you are having a blond moment :-) They are passing the var kopia with the value of document.getElementById("kopia").checked which is boolean in JS but when it gets to PHP via POST it is string "true" or string "false". Commented Mar 3, 2017 at 21:03

2 Answers 2

3

Your are passing true or false in $_POST['kopia'] via the return of document.getElementById("kopia").checked, however $_POST values are strings in PHP. So both string "true" and string "false" will evaluate to boolean true in your current if condition. You can check the string value:

$checkbox = $_POST['kopia'];
if ($checkbox == "true"){
    echo $checkbox;
} else {
    echo "bye";
}

Or you can convert them to a boolean, see PHP: Validate Filters (thanks to Sysix):

$checkbox = filter_var($_POST['kopia'], FILTER_VALIDATE_BOOLEAN);

Or if you set $_POST['kopia'] to 0 or 1, those strings will be evaluated correctly in your current if condition.

Or you could abandon the JS value switching and just check if $_POST['kopia'] exists (only when checked):

if(isset($_POST['kopia'])) {
    echo "hey";
} else {
    echo "bye";
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's the thing! I'm more of a js guy, so I didn't know that $_POST values are turned to strings. Thanks!
he can also transform the string into a boolean with $checkbox = filter_var($_POST['kopia'], FILTER_VALIDATE_BOOLEAN);
@Sysix: Right on, I always forget that one.
0

You could make a simple check like this:

$isChecked = !empty($_POST['kopia']) ? true : false;
if($isChecked){
    echo 'Checked';
}
else{
    echo 'Not checked';
}

empty checks both isset and if 0 it equals false, if 1 it equals true and if not set it equals false.

Hopes this helps.

1 Comment

Except it will never be empty given OP's description and code.

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.