简体   繁体   English

javascript中的重定向无法正常工作

[英]redirecting in javascript isn't working

<?php
session_start();
include ("dbconnectie.php");
if(isset($_SESSION['on'])) {
header("location:homepage.php"); }
if(isset($_POST['login'])) {
      $username = $_POST['username'];
      $password = sha1($_POST['password']);
      $query = $db->prepare("SELECT * FROM account
                            WHERE username = :user
                            AND password = :pass"); 
      $query->bindParam("user", $username);
      $query->bindParam("pass", $password);
      $query->execute();
      $result = $query->fetch(PDO::FETCH_ASSOC);
      $id1 = $result['id_u'];
      if($query->rowCount() == 1) {
      $_SESSION['on'] = $username;
      $_SESSION['id_u'] = $id1;
      //header('location: homepage.php');
      } else {
        echo "The username and password do not match";
      }
        echo "<br>";
    }
?>
<html>
    <head>
    <title>L O G I N</title>
    <link rel="shortcut icon" href="images/jfk.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="css/signin.css">
    <link rel="stylesheet" type="text/css" href="css/snackbar.css">
    </head>
    <header>
         <?php
            include("#nav.php");

        ?>
    </header>
<form class="modal-content" method="post" action="">
    <div class="container">
    <h1>I N L O G G E N</h1>
    <hr>

    <label>G E B R U I K E R S N A A M</label>
    <input type="text" name="username" placeholder="Gebruikersnaam Invullen"><br>

    <label>P A S S W O R D</label>
    <input type="password" name="password" placeholder="Password Invullen"><br>

    <button type="submit" class="submit" name="login" value"login" onclick="myFunction()">INLOGGEN</button>
    <div id="snackbar">U bent Ingelogd.</div>
    </div>
    <script>
    function myFunction() {
    // Get the snackbar DIV
    var x = document.getElementById("snackbar");

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
    window.location.href = "homepage.php";
    }
    //window.location.assign("homepage.php");
    </script>

</form> 
</html>

All the way at the bottom inside the HTML script, I have window.location.assign to try and redirect after logging and showing the snackbar, but in stead of that it just adds a white bar at the top and doesn't redirect. 在HTML脚本的底部,我一直使用window.location.assign来尝试在登录并显示小吃栏后重定向,但取而代之的是,它只是在顶部添加了一个白色栏,并且没有重定向。 What I want it to do is after clicking log in, it runs that script. 我要它执行的是单击登录后,它运行该脚本。 Which does a little pop up for 3 seconds and then redirect to the homepage. 这会弹出3秒钟,然后重定向到主页。

You can do event.preventDefault() so the default submit action of your submit button is disabled and all your needed code is ran. 您可以执行event.preventDefault()以便禁用“提交”按钮的默认提交动作,并运行所有需要的代码。

function myFunction() {
  // Get the snackbar DIV
  var x = document.getElementById("snackbar");

  // Add the "show" class to DIV
  x.className = "show";

  // After 3 seconds, remove the show class from DIV

  setTimeout(function() {
    x.className = x.className.replace("show", "");
    window.location.href = "homepage.php";
  }, 3000);

}
document.querySelector('button[name="login"]').addEventListener("click", function(event) {
  event.preventDefault()
  myFunction();
});

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

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