PHP表单验证失败

时间:2017-04-01 01:28:44

标签: php forms validation

我正在尝试使用php进行表单字段验证。我正在检查是否先设置提交按钮,然后如果表单字段为空,则将其保存在错误变量中,否则如果它不为空我有一个函数正在执行som表单清理并将值设置为textfield。然后提交给另一个页。

但我的错误处理从未发生过,带有空字段的表单会提交到我的操作页面网址。

代码有什么问题:

 <html>
<head>
    <meta charset="UTF-8">
    <title></title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
 <?php
 $fnameErr = "";
 $lnameErr = "";
 $Fname = "";
 $Lname = "";

if(isset($_POST["submit"])) {

if (empty($_POST["FirstName"])) {
$fnameErr = "First name is required";
} else {
$Fname = form_input($_POST["FirstName"]);
}

if (empty($_POST["LastName"])) {
$lnameErr = "Last name is required";
} else {
$Lname = form_input($_POST["LastName"]);
}

 }

function form_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
 ?>
 <h2>PHP Form Validation Example</h2>
  <p><span class="error">* required field.</span></p>
    <form action="intro.html.php" method="POST">
        Förnamn: <input type="text" name="FirstName">
        <span class="error">* <?php echo $fnameErr;?></span>
        <br>
        Efternamn: <input type="text" name="LastName">
        <span class="error">* <?php echo $lnameErr;?></span>
        <br>
        <input type="submit" name="submit" value="Skicka">
    </form>

    </body>
    </html>

谢谢

1 个答案:

答案 0 :(得分:0)

<强>更新

如果行动表格文件与表格文件不同,则需要检查行动中的输入。

这里是index.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .error {color: #FF0000;}
        </style>
    </head>
<body>
<?php
    /* get the return value when is not valid on into.html.php*/
    $fname = $_GET['fName']; $lname = $_GET['lName'];
    if($_GET['fName']=='false'){
        $fnameErr = "First name is required"; /*set value to show  notif*/
        $fname ="";
    }
    if($_GET['lName']=='false'){
        $lnameErr = "First name is required"; /*set value to show  notif*/
        $lname    ="";
    }
?>
<h2>PHP Form Validation Example</h2>
    <form action="intro.html.php" method="POST">
        Förnamn: <input type="text" name="FirstName" value="<?php echo $fname;?>"> 
        <span class="error">* <?php echo $fnameErr;?></span>
        <br>
        Efternamn: <input type="text" name="LastName" value="<?php echo $lname;?>">
        <span class="error">* <?php echo $lnameErr;?></span>
        <br>
        <input type="submit" name="submit" value="Skicka">
    </form>
    </body>
</html>

然后像这样设置你的intro.html.php

<?php
function form_input($data) {
    if($data!=""){ /*add condition if data != "" then run so we don't need more else on section if $_POST is empty*/
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
    }
    return $data;
}

if(isset($_POST["submit"])) { /*check the submit*/

    $Fname = form_input($_POST["FirstName"]); /* get post data from input val to variable $Fname*/
    $Lname = form_input($_POST["LastName"]); /* get post data from input val to variable $Lname*/

    if(!empty($_POST["FirstName"]) and !empty($_POST["LastName"])!=""){ /*cek if FirstName and LasName is not empty*/
        /* do what you want to do here*/
        echo ' FName : '.$Fname.' | LName : '.$Lname;
    }
    else{
        /*we will redirect to index.php using javascript*/
        echo '
            <script>
            alert("all input must be field!");
            window.location.href="index.php?fName='.($_POST["FirstName"]==""?'false':$Fname).'&lName='.($_POST["LastName"]==""?'false':$Lname).'"; /* this ridect to index.php and set variable  $_GET fName and variable lName */
            </script>
        ';
    }

}
else{
    echo 'submit is undefined!'; /*echo submit empty, */
}

?>

如果您想在提交前检查输入,可以使用HTML <input>必需的属性here the documentation

或者您可以使用jquery来伪造ajax提交,或者只是检查您在here

中可以看到的更多输入值 希望这有帮助。