在PHP勾选框中验证数据

在PHP勾选框中验证数据,php,forms,validation,Php,Forms,Validation,如果我有一个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

如果我有一个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>
当我没有点击t和c时,它应该说“点击这里确认…”。
目前,它只显示“系统错误”,因此POST数组中无法识别“术语”名称键。有什么想法吗?

下面更正了双=号“==”的比较

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

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

您可以这样验证:

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

if(isset($\u POST['terms'])和空($\u POST['terms']){echo“error”}否则{echo“success”}@devpro unticked勾选框从未设置过……否则它将不会是一个比较。它将是一个赋值,并且总是求值为true。@如果表达式返回false,则不求值。他正在检查赋值本身的值,而不是表达式的值。这是完全正确的。你的(if(empty(..)的目的是什么?未选中的复选框将不会被定义,您不需要这段代码只是为了检查。@Sakuto:u r right my friend,这里没有需要,在OP的ist更新中没有输入字段,所以我做了…谢谢friend…答案更新。它的回音也没有真正的用处,因为OP只需要检查它是否被选中。
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";
}
?>