3

$data is always true and checkboxes return always "YES". How to solve this problem?

function yesNo($data){
    if($data){
        return "YES";
    }else{
        return "NO";
    }
}

Checkboxes here:

$txt.="Rozpoczynam działalność gospodarczą: ".yesNo($_POST['indywidualne'])."<br>";
if(!isset($_POST['komunikacja'])||!trim($_POST['komunikacja'])){
    $result = array( 'type' => 'error', 'code' => "podaj komunikacje");
    endSend($result);
}
$txt.="Chce rozliczać się w terminach kwartalnych: ".yesNo($_POST['komunikacja'])."<br>";
if(!isset($_POST['firmowe'])||!trim($_POST['firmowe'])){
    $result = array( 'type' => 'error', 'code' => "podaj rodzaj rozliczenia o");
    endSend($result);
}
$txt.="Płatnik VAT: ".yesNo($_POST['firmowe'])."<br>";
if(!isset($_POST['ewidencja'])||!trim($_POST['ewidencja'])){
    $result = array( 'type' => 'error', 'code' => "podaj email");
    endSend($result);
}

Checkbox in html:

          <label for="checkbox-vat">
              <input type="checkbox" name="" id="checkbox-vat"> Płatnik VAT
          </label>
          <label for="checkbox-activity">
              <input type="checkbox" name="" id="checkbox-activity"> Rozpoczynam działalność gospodarczą
          </label>
          <label for="checkbox-quarterly">
              <input type="checkbox" name="" id="checkbox-quarterly"> Chce rozliczać się w terminach kwartalnych
          </label>
pomor
  • 31
  • 2

2 Answers2

6

As I have seen, you are passing some value in $data variable. And the IF condition considers it as a value, any in IF statement will treat as a TRUE.

Checkbox returns the value YES or NO. The value of checkbox YES or NO for IF statement is always TRUE.

Just change to:

    function yesNo($data){
    // echo $data;die; //uncomment this statement to see $data value
        if(strtolower($data)=='yes'){
            return "YES";
        }else{
            return "NO";
        }
    }

Suppose you have checkbox field as below:

<input type="checkbox" name="check_yes" value="yes" />
<input type="checkbox" name="check_no" value="no" />

You will receive checkbox value as yes or no. Please put your checkbox field here or write what value you have given for it.

Updated Answer: If checked the checkbox, you will get checkbox value if not then the checkbox name does not exist in $_POST or $_GET data array. As I have seen you didn't provide name and value for checkboxes. So your case the checkboxes names and values do not exist in $_POST or $_GET data array.

The best example of how to use checkboxes in PHP

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
0

If your code doesn't display any error, it means you checkbox is always passing some value to post. You should check first if the post index is set

yesNo( isset($_POST['indywidualne']) )

ACD
  • 1,431
  • 1
  • 8
  • 24
  • Still always "YES" – pomor Oct 19 '18 at 09:50
  • this will always pass - isset() will just check if var is set, and it always will be (regardless of checkbox value) – treyBake Oct 19 '18 at 10:33
  • @pomor eh, u dont have name in your checkboxes? $_POST is using input's name attribute as index. where do u even get the index "indywidualne, komunikacja, etc"? – ACD Oct 19 '18 at 16:56