如何在php脚本中验证单个复选框
在下面的代码中,我有一个复选框,我不知道如何验证复选框 我有一个复选框,如果它是取消选中它应该给消息,请接受协议 请帮助我的朋友如何在php脚本中验证单个复选框,php,Php,在下面的代码中,我有一个复选框,我不知道如何验证复选框 我有一个复选框,如果它是取消选中它应该给消息,请接受协议 请帮助我的朋友 <?php $firstname = $lname = ""; $firstnameErr = $lnameErr = ""; if ($_SERVER['REQUEST_METHOD']== "POST") { $valid = true; /*FirstName Validation starts here*/ if(
<?php
$firstname = $lname = "";
$firstnameErr = $lnameErr = "";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
/*FirstName Validation starts here*/
if(empty($_POST["fname"])) {
$firstnameErr = "*firstname is Required";
$valid=false;
} else {
$firstname = test_input($_POST["fname"]);
}
/*LastName Validation starts here*/
if(empty($_POST["lname"])) {
$lnameErr = "*lastname is Required";
$valid=false;
} else {
$lname=test_input($_POST["lname"]);
}
if (isset($_POST['confirm'])) {
// do something
}
//if valid then redirect
if($valid){
echo 'success';
exit;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="example.php">
firstname<input type="text" name="fname"/><?php echo $firstnameErr?><br /><br />
lastname<input type="text" name="lname"/><?php echo $lnameErr?><br /><br />
<input type="checkbox" name="agree" />
Agree the terms and condition
<input type="submit" value="Submit" />
</form>
名字
lastname
同意条款和条件
请帮助我的朋友们如果选中复选框,它将被传递到您的PHP,否则不会,所以只需使用
isset()
函数:
<?php
$firstname = $lname = "";
$firstnameErr = $lnameErr = "";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
/*FirstName Validation starts here*/
if(empty($_POST["fname"])) {
$firstnameErr = "*firstname is Required";
$valid=false;
} else {
$firstname = test_input($_POST["fname"]);
}
/*LastName Validation starts here*/
if(empty($_POST["lname"])) {
$lnameErr = "*lastname is Required";
$valid=false;
} else {
$lname=test_input($_POST["lname"]);
}
if (isset($_POST['confirm'])) {
// do something
}
//if valid then redirect
if($valid){
echo 'success';
exit;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="example.php">
firstname<input type="text" name="fname"/><?php echo $firstnameErr?><br /><br />
lastname<input type="text" name="lname"/><?php echo $lnameErr?><br /><br />
<input type="checkbox" name="agree" />
Agree the terms and condition
<input type="submit" value="Submit" />
</form>
if (!isset($_POST['agree'])) {
echo "Please accept the agreement";
}
请通读下面的代码。我认为它会很好地工作。
<?php
$firstname=$lname="";
$firstnameErr=$lnameErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
/*FirstName Validation starts here*/
if(empty($_POST["fname"]))
{
$firstnameErr="*firstname is Required";
$valid=false;
}
else
{
$firstname=test_input($_POST["fname"]);
}
/*LastName Validation starts here*/
if(empty($_POST["lname"]))
{
$lnameErr="*lastname is Required";
$valid=false;
}
else
{
$lname=test_input($_POST["lname"]);
}
if(empty($_POST["agree"]))
{
$agreeErr="*check box is Required";
$valid=false;
}
else
{
$agree=test_input($_POST["agree"]);
}
if (isset($_POST['confirm'])) {
// do something
}
//if valid then redirect
if($valid){
echo 'success';
exit;
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="example.php">
firstname<input type="text" name="fname"/><?php echo $firstnameErr?><br /><br />
lastname<input type="text" name="lname"/><?php echo $lnameErr?><br /><br />
<input type="checkbox" name="agree" /><?php echo $agreeErr?>
Agree the terms and condition
<input type="submit" value="Submit" />
</form>
名字
lastname
同意条款和条件
$agree=$\u POST['agree'];为复选框指定值。然后运行一个if语句--if($_POST['agree']==您的值){run compliance code}else{error message}@dorvala nope:在检查是否设置之前直接访问$_POST['agree'],当有人没有单击收音机时将抛出一个错误button@ReinderWit啊,我的坏朋友,一进入页面就忘了那个部分。