[英]Determine whether a checkbox is checked upon form submission
i have been looking for this script for a while now. 我一直在寻找这个脚本已有一段时间了。 I have some rules and then i have a checkbox to click if you agree the terms and rules.
我有一些规则,然后如果您同意条款和规则,则有一个复选框可以单击。
Now how do i make a check in PHP if the person has checked that box and agreed to the rules? 现在,如果该人选中了该框并同意规则,我该如何在PHP中进行检查? Thanks for ur help, i appreciate it!
感谢您的帮助,我非常感谢!
Assuming you have a form that looks something like this: 假设您的表单看起来像这样:
<form method="post" action="some_handler.php">
<label for="option1">Option 1</label>
<input id="option1" type="checkbox" name="option1" />
<label for="option2">Option 2</label>
<input id="option2" type="checkbox" name="option2" />
<!-- submit, etc -->
</form>
You can check for the presence of the checkbox values (by name) in $_POST
, ie 您可以检查
$_POST
是否存在复选框值(按名称),即
<?php
$optionOne = isset( $_POST['option1'] );
$optionTwo = isset( $_POST['option2'] );
If the boxes aren't checked, $_POST
won't contain values for them. 如果未选中这些框,则
$_POST
将不包含它们的值。
It's totally enough to check for: 检查以下内容已足够:
$userAgrees = false;
if (isset($_POST['myCheckbox']))
{
$userAgrees = true;
}
If the form is a method POST form. 如果表单是方法POST表单。 Then on the action page you should have access to the $_POST variable.
然后,在操作页面上,您应该可以访问$ _POST变量。
Check out the results of this on your action page. 在操作页面上查看结果。
echo "<pre>";
print_r($_POST);
echo "</pre>";
The $_POST variable will be an array. $ _POST变量将是一个数组。 You can access the value of the array like this.
您可以像这样访问数组的值。
if($_POST["key"] == "value")
Where the key is the name in the output above. 键是上面输出中的名称。
form.php: form.php的:
<form action="checkbox-form.php" method="post">
<label for="formWheelchair">Do you need wheelchair access?</label>
<input type="checkbox" name="formWheelchair" value="Yes" id="formWheelchair" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
checkbox-form.php: 复选框-form.php的:
if(isset($_POST['formWheelchair']) &&
$_POST['formWheelchair'] == 'Yes')
{
$wheelchair = true;
}
else
{
$wheelchair = false;
}
var_dump( $wheelchair );
// shorthand version: //简写版本:
$wheelchair = isset($_POST['formWheelchair'])?true:false;
Straight from: http://www.html-form-guide.com/php-form/php-form-checkbox.html 直接来自: http : //www.html-form-guide.com/php-form/php-form-checkbox.html
Note: At a later point you may want to use sessions to store the data for server-side validation if the user has not entered in all the fields. 注意:如果用户未在所有字段中都输入,则稍后可能需要使用会话来存储数据以进行服务器端验证。
In form html: 格式为html:
<input type="checkbox" name="terms">
In php script that the form posts to: 在表单中发布到的php脚本中:
if ( $_POST['terms'] == 'on' ) {
echo 'User accepted terms';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.