1
<input name="inform_me" id="inform_me" type="checkbox"> inform me

Whenever I check if this checkbox is set, I get

bool(true)

even if the checkbox is not really checked.

This is how my PHP looks like:

var_dump(isset($_POST['inform_me']));

What's wrong with it?

2
  • 2
    Isset checks if something is there or not. You are not checking if checkbox is checked but if checkbox exist.
    – pregmatch
    Nov 14, 2015 at 22:55
  • Did you try to get its value from the javascript console in the browser before submitting the form? Remember the value you're trying to get is the $_POST['inform_me'], isset only checks if the POSTED form brings that field Nov 14, 2015 at 22:55

3 Answers 3

5

The PHP isset() function will check if the variable exists!

For example.

$a = false; // or any other value
echo isset($a); // True, because the variable $a exists!
echo isset($b); // False, because the variable $b doesn't exist!

No need to put the isset there, try the following:

echo $_POST['inform_me'];

The $_POST['inform_me'] variable will contain the value. If it is checked, it will contain the value that is the value of your checkbox.

For example this is the HTML code

<input name="inform_me" id="inform_me" type="checkbox" value="test"> inform me </input>

Then the variable $_POST['inform_me'] will be "test" if checked, otherwise (if unchecked) the value will be an empty string.

In case you didn't provide the HTML tag with a value, the variable will be "on" if checked, otherwise an empty string (if unchecked).

4
  • @HasseBjörk This depends on your code. It will take the value attribute of the checkbox if checked. Otherwise it'll be an empty string. Nov 14, 2015 at 22:59
  • 1
    Thank you! If value attribute is not set, it will return "on" or empty string if unchecked! Nov 14, 2015 at 23:03
  • @HasseBjörk Right! Added that into my answer. Nov 14, 2015 at 23:09
  • My problem was in the Ajax code. I have posted the answer below. Thanks for your answer, you are absolutely right though. Nov 14, 2015 at 23:23
1

I found the solution. My problem was that I passed the variable $_POST['inform_me'] with Ajax. In Ajax I used data: { inform_me: $('#inform_me').val() } to get the value of the checkbox. Instead of .val() I am using .is(':checked') now and it works!

0

Well as far as I know in my short experience checkbox return on or off (so as you take it it will always return true because it exist but you're not especifing the return conditioned to the value it have), as said before. Even so, there is a way I have successfully used to condition that, in my page I have a checkbox for input if you want RSS or not and I store 1 if the want RSS (mode on) or 0 if the don't. Being for your case like this:

($_POST['inform_me'] == 'on') ? 1 : 0;

So far you can change one and 0 to whatever return you want to get back, remembering that the first one (1 in these case) will be the return if condition is true and the second (0 for the example) if condition is false.

2
  • on is not a constant, but a string. It should be "on". I don't think the "off" value exists, but I could be wrong. Nov 14, 2015 at 23:23
  • Actually you are right and I have already edited to correct that, thanks a lot.
    – Ecangis
    Nov 14, 2015 at 23:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.