【问题标题】:PHP : Validation errors pops up on the loading pagePHP:加载页面上弹出验证错误
【发布时间】:2019-12-27 09:33:08
【问题描述】:

我目前正在创建一个登录表单。我希望在点击“登录”后显示验证错误

我已经加了if(isset($_POST['login']))

这是我的查询代码

<?php

session_start();
$mail = $pass = "";
$errors = array();

$db = mysqli_connect('192.x.y.z','root','1234','LUCA','3306');


if(isset($_POST['login'])){
    $mail = mysqli_real_escape_string($db,$_POST['email']);
    $pass = mysqli_real_escape_string($db,$_POST['password']);
}

    if(empty($mail)){
        array_push($errors, "Email is required");
    }
    if(empty($pass)){
        array_push($errors, "Password is required");
    }

    if(count($errors) == 0 ){
        $pass = $pass;
        $query = "SELECT * FROM login where email ='$mail' AND pw='$pass'";
        $result = mysqli_query($db, $query);

        if (mysqli_num_rows($result) == 1)
        {
            $_SESSION['email'] = $mail;

            header('home.html');
            exit();

        }else{
            array_push($errors, "Wrong Email/Password");
        }
    }

?>

这是我的表格

<?php include('server.php');

?>

<!DOCTYPE html>
<html>

<head>
  <title>Student Zone | LUCA Academy</title>
  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="js/login.js"></script>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <link rel="icon" type="image/png" href="img/favicon.png">
</head>

<body>
  <div class="login-reg-panel">
    <div class="login-info-box">
      <label id="label-register" for="log-reg-show">Login</label>
      <input type="radio" name="active-log-panel" id="log-reg-show" checked="checked">
    </div>

    <div class="register-info-box">
      <img src="img/luca-lg.png">
    </div>
    <div class="white-panel">
      <div class="login-show">
        <form action="index.php" method="post">
          <?php include('validation.php'); ?>
          <h2>LOGIN</h2>
          <input type="text" placeholder="Email" name="email">
          <input type="password" placeholder="Password" name="password">
          <input type="submit" name="login">
          <a href="">Forgot password?</a>
        </form>
      </div>
    </div>
  </div>
</body>
</html>

<?php if (count($errors) > 0): ?>

<div class="error">
  <?php foreach($errors as $error): ?>
  <p>
    <?php echo $error ?>
  </p>
  <?php endforeach ?>

</div>

<?php endif ?>

提前感谢您的任何建议和帮助

【问题讨论】:

  • 我删除了 JS 和 HTML。你的问题都没有。我还删除了 sn-p。它不可运行。表单是什么样子的,数据是如何发送到服务器的?
  • @mplungjan 我已经编辑了问题并添加了表单
  • 您应该将验证包装在一个函数上并在表单提交时调用该函数
  • 感谢大家的帮助!我已经设法自己解决了。这是一个大括号提前关闭
  • 不要在数据库中存储明文密码。请改用password_hash()

标签: php html mysql validation


【解决方案1】:

您可以使用 index.php 中的会话变量携带错误

if (!empty($errors)) {
   $_SESSION['errors'] = $errors;
} else {
   $newURL = 'http://your-after-login-page.com/after-login';
   header('Location: '.$newURL);
}

然后在登录页面中检查错误并显示相应的消息

<div class="login-show">
    <form action="index.php" method="post">
        <?php include('validation.php'); ?>
            <h2>LOGIN</h2>
            <?php
            if(isset($_SESSION['errors']) && !empty($_SESSION['errors'])){
                echo '<ul>';
                foreach($_SESSION['errors'] as $error){
                    echo '<li style="color: #A52A2A;">' . $error . '</li>';
                }
                echo '</ul>';
            }
        ?>
        <input type="text" placeholder="Email" name="email">
        <input type="password" placeholder="Password" name="password">
        <input type="submit" name="login">
        <a href="">Forgot password?</a>
    </form>
</div>

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2017-10-21
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多