验证 PHP 复选框中的数据


Validating data in PHP Tick box

如果我在 HTML 中有一个表单需要单击按钮才能接受 T 和 C,如何在 PHP 中验证这一点?我从

<div>            
     <input type="checkbox" name="terms" id="tandc" value="yes" />
     <label for="tandc">Tick this box to confirm you have read our a href="#">terms and conditions</a></label>
</div>

然后

            if(isset($_POST['terms'])) {
                $t_and_c = $_POST['terms'];
                    if ($t_and_c = empty($_POST['terms'])) {
                        echo'please click to confirm the T's and C's';
                    } else {
                        echo htmlentities($t_and_c);
                    }
                } else {
                echo'system error';
                }

当我不点击 t 和 c 时,它应该说"点击这里确认......"。目前,它只显示"系统错误",因此在 POST 数组中无法识别"术语"名称键。有什么想法吗?

if(isset($_POST['terms']))
    // user checked it

如果未选中,则不会设置变量。

您可以像这样进行验证:

<?php
if(isset($_POST['terms'])) 
{
    echo htmlentities($_POST['terms']);
} 
else {
    echo "please click to confirm the T's and C's";
}
?>

你的代码有什么问题:

  • $t_and_c = empty($_POST['terms']您没有比较这是分配值。

  • echo'please click to confirm the T's and C's';这不起作用,因为您在单引号中使用单引号。

比较是双=符号"=="在下面更正

if ($t_and_c == empty($_POST['terms']))