PHP 表单提交:"false" 未通过

PHP Form Submission: "false" not passed in

我有一个将用户输入插入 MySQL 数据库的表单,其中一个输入字段具有布尔值。当值为 true 时它工作正常,但是如果值为 false,我的 'echo' 和数据库根本无法接收该值。我真的不知道发生了什么。请看下面我的代码: HTML:

                <label>
                  <input type="checkbox" id="consentForEmail" name="consentForEmail" />&nbsp;
                  <span class="wpcf7-list-item-label">By clicking this button I agree to receive emails from us</span>
                </label>

JavaScript 用于收集和设置值:

    var _this = '#consentForEmail';

    $('#consentForEmail').change(function(){
      var checked = $('#consentForEmail').prop('checked');
      $(_this).val(checked);
      console.log( $('#consentForEmail').val() );
    });

最后,PHP 用于收集表单数据:

$receive_email = $_POST["consentForEmail"];
echo "<li>$receive_email</li>"

谢谢!

复选框如果未选中,则不会随表单一起提交,无论它们的值是多少。虽然您可以在复选框 <input> 上附加一个值,但要检查的真正测试是输入的 name 是否完全出现:

if (isset($_POST['consentForEmail'])) { 
   ... box was checked 
} else {
   ... box was NOT checked
}