PHP表单忽略必填字段

时间:2016-09-08 20:20:06

标签: php forms validation post required

我有一个php表单,但即使没有填写必填字段也会提交。任何人都可以找出原因吗?我不是一个php开发人员,我不确定它为什么不工作。请告诉我在哪里添加您可能提供的任何代码段,因为这对我来说是新的。为了证明我已经尝试过,并且在某种程度上知道我在说什么,我只是在这里显示与该问题相关的代码。

<?php

 require 'xxxxxxx.php';


$fnameErr = "";


         if(isset($_POST['add'])) {


        if (empty($_POST["fname"])) {
    $fnameErr = "Name is required";
    exit();
}else {
   $fname = check_input($_POST['fname']);  
  }


        $lname = check_input($_POST['lname']);  
        $phone = check_input($_POST['phone']);  
        $email = check_input($_POST['email']);  


 $sql = " INSERT INTO QuotesInfo ( fname, lname, phone, email, doservice, etype, rtype, 
 ptime, paddress, rtime, daddress, vtype, pcount, addetails, heardwhere )  
 VALUES ('".$_POST["fname"]."','".$_POST["lname"]."','".$_POST["phone"]."','
".$_POST["email"]."','".$_POST["doservice"]."','".$_POST["etype"]."','".$_POST["rtype"]."','".$_POST["ptime"]."','
".$_POST["paddress"]."','".$_POST["rtime"]."','".$_POST["daddress"]."','".$_POST["vtype"]."','".$_POST["pcount"]."','
".$_POST["addetails"]."','".$_POST["heardwhere"]."') "; 


$msg= $fname . " " . $lname . "\r\n" . $phone . "\r\n" . $email . "\r\n" . $doservice . "\r\n" . $etype . "\r\n" . $rtype . "\r\n" . $ptime . "\r\n" . $paddress . "\r\n" . $rtime . "\r\n" . $daddress . "\r\n" . $vtype . "\r\n" . $pcount . "\r\n" . $addetails . "\r\n" . implode(",  ", $heardwhere);
$headers = "From: $from";


<form id="form1" name="form1"  method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="fname">First Name</label><input type="text" name="fname" id="fname" />
<span class="error">* <?php echo $fnameErr;?></span>
<br class="clear" /> 

1 个答案:

答案 0 :(得分:0)

您的代码中存在大量语法错误。这是一个已清理的版本,请在使用前进行验证:

<?php

    require 'xxxxxxx.php';


    $fnameErr = "";


    if(isset($_POST['add'])) {

        if (empty($_POST["fname"])) {

            $fnameErr = "Name is required";

            exit();

        } else {

            $fname = check_input($_POST['fname']);  

        }

        $lname = check_input($_POST['lname']);  

        $phone = check_input($_POST['phone']);  

        $email = check_input($_POST['email']);  


        $sql = "INSERT INTO QuotesInfo 
                ( fname, lname, phone, email, doservice, etype, rtype, ptime, paddress, rtime, daddress, vtype, pcount, addetails, heardwhere )  
                VALUES 
                ('".$_POST["fname"]."',
                '".$_POST["lname"]."',
                '".$_POST["phone"]."','
                '".$_POST["email"]."',
                '".$_POST["doservice"]."',
                '".$_POST["etype"]."',
                '".$_POST["rtype"]."',
                '".$_POST["ptime"]."',
                '".$_POST["paddress"]."',
                '".$_POST["rtime"]."',
                '".$_POST["daddress"]."',
                '".$_POST["vtype"]."',
                '".$_POST["pcount"]."',
                '".$_POST["addetails"]."',
                '".$_POST["heardwhere"]."')"; 

        $msg= $fname . " " . $lname . "\r\n" . $phone . "\r\n" . $email . "\r\n" . $doservice . "\r\n" . $etype . "\r\n" . $rtype . "\r\n" . $ptime . "\r\n" . $paddress . "\r\n" . $rtime . "\r\n" . $daddress . "\r\n" . $vtype . "\r\n" . $pcount . "\r\n" . $addetails . "\r\n" . implode(",  ", $heardwhere);

        $headers = "From: $from";

?>


    <form id="form1" name="form1"  method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <label for="fname">First Name</label>
        <input type="text" name="fname" id="fname" required />
        <span class="error">* <?php echo $fnameErr;?></span>
        <br class="clear" /> 
    </form>
<?php } ?>

关于required验证,您需要在表单元素中添加required属性。