繁体   English   中英

PHP单页表单验证HTML-表单未验证

[英]PHP Single Page Form Validation HTML - form not validating

PHP部分,变量已初始化并设置为空。 以及post方法和isset函数

这些功能似乎是正确的,运行代码时没有错误。 但是,当用户提交所有内容时,不会进行任何处理。 这只是代码的一小部分。

<?php


//define variables and set them to empty values    

$fname_error= $phone_error= $address1_error= $address2_error= $city_error= $state_error= $zipcode_error= "";

$fname= $phone= $address1= $address2= $city= $state= $zipcode= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fname"])) {
        $fname_error = "Missing";
    }
    else {
        $fname = test_input($_POST["fname"]);
        //now we check to see that the name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
            $fname_error = "Please use letters and white space only";
        }
    }

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>

HTML部分:

                               <div class="userinput">
                                    <label for="fname"><b>First Name</b></label>
                                    <input type="text" name="fname" value="<?php 
                                         echo $fname ?>">
                                    <span class="error">
                                        <?php echo $fname_error;?></span>
                                </div>

关闭条件REQUEST_METHOD。 您的代码应如下所示:

 <!DOCTYPE HTML>  
  <html>
   <head>
    <style>
        .error {color: #FF0000;}
    </style>
</head>
<body>  

    <?php
  //define variables and set them to empty values    

    $fname_error = $phone_error = $address1_error = $address2_error = $city_error = $state_error = $zipcode_error = "";

    $fname = $phone = $address1 = $address2 = $city = $state = $zipcode = "";
    //flag to validate and allow SQL insert if true
    $valid=true;
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["fname"])) {
            $fname_error = "Missing";
            $valid=false;
        } else {
            $fname = test_input($_POST["fname"]);
            //now we check to see that the name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/", $fname)) {
                $valid=false;
                $fname_error = "Please use letters and white space only";
            }
        }
    }
    //filter your input for security reason
    function test_input($data) {
        $data1 = trim($data);
        $data2 = stripslashes($data1);
        $data3 = htmlspecialchars($data2);
        return $data3;
    }
    if($valid){
        //Add you insert SQL
    }
    ?>

    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">  
        <label for="fname"><b>First Name</b></label>
        <input type="text" name="fname" value="<?php echo $fname ?>">
        <span class="error">
            <?php echo $fname_error; ?></span>
        <br><br>

        <input type="submit" name="submit" value="Submit">  
    </form>

    <?php
    //For testing porpuses:
    echo "<h2>Your Input:</h2>";
    echo $fname;
    ?>

</body>

参考: https : //www.w3schools.com/php/php_form_complete.asp

表格验证

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM