PHP复选框验证,如果是其他问题

PHP复选框验证,如果是其他问题

问题描述:

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?

我的HTML文件中有一个复选框: p>

 &lt  ; input type =“checkbox”id =“kopia”name =“kopia”class =“form-input”value =“0”&gt; 
  code>  pre> 
 
 

我' 我试图传递给PHP文件(通过AJAX)一些值,其中一个是由复选框(简化)创建的布尔值: p>

  name:“kopia  “
value:document.getElementById(”kopia“)。checked 
  code>  pre> 
 
 

如果我通过控制台检查,如果选中,则返回 true code> 如果未选中,则为 false code>。 p>

  document.getElementById(“kopia”)。checked 
  code>  pre> 
 
 如果我通过PHP文件检查,它的行为相同: p> 
 
 
  echo $ _POST ['kopia'] 
  code>  pre> 
 
  

但是当我写这样的东西时: p>

  $ checkbox = $ _POST ['kopia']; 
if($ checkbox){
 echo“hey”;  
} else {echo“bye”;} 
  code>  pre> 
 
 

无论是否勾选复选框,它总是返回“嘿”。 我不明白。 即使我做了这样的事情: p>

  $ checkbox = $ _POST ['kopia']; 
if($ checkbox){
 echo $ checkbox; 
} else  {echo“bye”;} 
  code>  pre> 
 
 

它永远不会返回“bye”,无论是否选中该框,但返回 true code >如果选中,则如果未选中则 false code>。 这件事已经偷了几个小时,而且这个时间太长了几个小时。 任何想法,提示? p> div>

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

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.