表单提交时的PHP验证错误

时间:2015-04-30 00:27:45

标签: php

以下代码无法按预期工作,当单击表单的提交按钮时没有输入数据它会转到blog.php而不是在表单上方显示错误?

<?php 

session_start();

include_once('connection.php');



if (isset($_SESSION['logged_in'])){
    //display index
} else {
    if (isset($_POST['username'], $_POST['password'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if (empty($username) or empty($password)) {
            $error = 'All fields are required!';
        }
    }
}
?>

与以下html表单链接

  <?php if (isset($error)) { ?>
    <small style="color:#aa0000;"><?php echo $error;  ?> </small>
  <?php } ?>


  <form action="blog.php" method="post">
    <input type="text" name="username" placeholder="username" />
    <input type="password" name="password" placeholder="password" />
    <input type="submit" value="login" />
  </form>

1 个答案:

答案 0 :(得分:1)

如果在admin.php中进行了实际验证,那么action是否应该指向admin.php

  <?php if (isset($error)) { ?>
    <small style="color:#aa0000;"><?php echo $error;  ?> </small>
  <?php } ?>


  <form action="admin.php" method="post">
    <input type="text" name="username" placeholder="username" />
    <input type="password" name="password" placeholder="password" />
    <input type="submit" value="login" />
  </form>