我在我的PHP页面上有一个复选框,当我发布回页面时,我只想在用户已经勾选它的情况下勾选复选框。我正在使用下面的代码行,但这总是选中复选框,即使用户自己没有选中它。我该怎么做呢?
<input type="checkbox" name="terms" id="terms" <?php if (isset($_POST['terms']) == true) echo('checked="checked"'); ?> /> I agree to the Terms as stated above.
您需要检查$_POST['terms'] value
将value设置为该输入元素,并在php中检查该值:
<input type="checkbox" name="terms" id="terms" value="1"
<?php if (isset($_POST['terms']) && $_POST['terms'] == 1) echo('checked="checked"'); ?>
/> I agree to the Terms as stated above.
尝试编写POST数组并每次检查'term'的值。print_r($_POST);
你的语句格式正确,错误一定在别处
确保你是用表单发布的,而不是使用get
<form action="post" >
留空就像是在说
<form action="get" >
查看url,当您有< input name="terms" checked="checked"/>
时,这是isset($_POST['terms'])
为假的唯一方法,除非您实际上没有检查它。