如何在php脚本中验证单个复选框

时间:2014-02-26 10:54:28

标签: php

在下面的代码中,我有一个复选框,我不知道如何验证复选框。

我有一个复选框,如果取消选中它应该给出消息,请接受协议。

请帮助我的朋友

<?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>

请帮助我的朋友

2 个答案:

答案 0 :(得分:0)

如果选中复选框,它将被传递给您的PHP,否则它将不会,所以只需使用isset()函数:

if (!isset($_POST['agree'])) {
    echo "Please accept the agreement";
}

答案 1 :(得分:0)

请仔细阅读以下代码。我认为它会正常工作。

  <?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>