0

I got this form:

<form name="formx" id="formx" action="var.php" method="POST">
  <input type="checkbox" name="f_check1"> Check 1
  <input type="checkbox" name="f_check2">Check 2
  <input name="f_register" value="Register" type="submit">
</form>

And on the var.php file I have:

   <?php
   if($_POST['f_register'] == "Register") {
     $check1 = $_POST['f_check1'];
     $check2 = $_POST['f_check2'];
     }

 echo $check1. "<br>" ;
 echo $check2;
?> 

And when i fill the form and go to the var.php, i only get results if the checkbox is on, and i want it to say "true" if checked and "false" if not.

P.S: I'm using XAMPP to run the website.

Berna
  • 39
  • 9

4 Answers4

1
$check1 = isset($_POST['f_check1']);
$check2 = isset($_POST['f_check2']);
dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 1
    Um... this won't work. It's always going to be considered as being set. – Funk Forty Niner Mar 31 '16 at 16:40
  • @Fred-ii- This should work. Checkboxes send the value `on` if they're checked. Otherwise they don't get sent to the server at all. – dokgu Mar 31 '16 at 16:45
  • http://stackoverflow.com/questions/11424037/does-input-type-checkbox-only-post-data-if-its-checked – dokgu Mar 31 '16 at 16:46
  • That's the thing, i want then to "send" even if they are not checked :) – Berna Mar 31 '16 at 16:47
  • @Berna So that's why my answer tests that. If it is set, you get a value of `true`, otherwise you get a value of `false`. – dokgu Mar 31 '16 at 16:49
1

Unchecked checkboxes aren't sent to the server. So you can account for that with:

 $check1 = isset($_POST['f_check1']) ? true:false;
 $check2 = isset($_POST['f_check2']) ? true:false;
j08691
  • 204,283
  • 31
  • 260
  • 272
  • What about it doesn't work? Are you expecting to see something when the value is false? PHP doesn't echo anything for false. – j08691 Mar 31 '16 at 16:45
  • 1
    No need to use ternary operators when `isset` already returns a boolean. – GGG Mar 31 '16 at 16:48
  • @GGG - totally true, however for a novice PHP developer this is more clear and the end result is the same. – j08691 Mar 31 '16 at 16:49
  • `isset()` will return false, because the value of the checkbox evaluates to false (NULL). You need to set a value in the HTML code for the checkbox - ` Check 1` – omnichad Mar 31 '16 at 16:50
1

Use this block:

 <?php
   if($_POST['f_register'] == "Register") {
     $check1 = isset($_POST['f_check1']);
     $check2 = isset($_POST['f_check2']);
     }

if($check1) echo '<br>check1 true';
else echo 'check1 false';
if($check2) echo '<br>check2 true';
else echo '<br>check2 false';
?> 
Jhonny Mesquita
  • 148
  • 2
  • 9
1

Try this?

<form name="formx" id="formx" action="var.php" method="POST">
<input type="checkbox" name="f_check1"> Check 1
<input type="hidden" name="f_check1" value="0" />
<input type="checkbox" name="f_check2">Check 2
<input type="hidden" name="f_check2" value="0" />
<input name="f_register" value="Register" type="submit">
</form>

Edited as requested:

The hidden field with the same name will be passed if the checkbox is not checked.

  • 1
    This would be the most useful answer, if it had an explanation of WHY it works as expected. Please add an explanation of WHY having these fields performs a fallback when the checkbox is not set. – Luis Masuelli Mar 31 '16 at 17:04