Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS成功登录后重定向到另一个HTML页面_Javascript_Html_Authentication - Fatal编程技术网

Javascript 使用JS成功登录后重定向到另一个HTML页面

Javascript 使用JS成功登录后重定向到另一个HTML页面,javascript,html,authentication,Javascript,Html,Authentication,我已经创建了一个登录表单。我想要一个JS代码: 如果登录页面上的信息(即用户名和登录名)正确,它将转到另一个HTML页面。 简单地说,我希望页面在成功登录后重定向到另一个页面用JS 我不想要任何PHP或MySql代码 我已经尝试过这个代码,但它不起作用。如果信息错误,则表示信息无效。但是在正确的信息下,它会刷新页面,并且有一个?在url的末尾可见: Html: 我还使用了: window.location.replace("upload.html"); 而不是: loc

我已经创建了一个登录表单。我想要一个JS代码:

  • 如果登录页面上的信息(即用户名和登录名)正确,它将转到另一个HTML页面。 简单地说,我希望页面在成功登录后重定向到另一个页面用JS

  • 我不想要任何PHP或MySql代码

    我已经尝试过这个代码,但它不起作用。如果信息错误,则表示信息无效。但是在正确的信息下,它会刷新页面,并且有一个?在url的末尾可见:

  • Html:

    我还使用了:

    window.location.replace("upload.html");
    
    而不是:

    location = location['href'];
    

    但仍然不起作用。

    这里有几件事。一个你正在用你的表单做一个
    GET
    ,而不是做一个
    POST
    。你应该改变这一点。其次,在JavaScript调用
    auth
    时,应该传递
    事件
    参数,以便停止表单的默认操作。第三,您应该在重定向中为
    URL
    使用相对路径。最后,应该执行一个
    window.redirect()
    ,在函数执行时模拟HTTP重定向

    以下是更新代码:

    <form id="form" action="post">
        <div class="form-group">
           <label for="exampleInputEmail1">Email address</label>
           <input type="email"  
                  id="email" 
                  class="form-control"  
                  aria-describedby="emailHelp" >
            <small id="emailHelp" 
                    class="form-text text-muted">
                 We'll never share your email with anyone else.
            </small>
        </div>
        <div class="form-group">
           <label for="exampleInputPassword1">Password</label>
           <input type="password" id="password" class="form-control" >
        </div>
        <button type="submit" 
                 class="btn btn-outline-success btn-lg shadow" 
                 onClick="auth(event)">Submit</button>
     </form>
     <script>
        function auth(event) {
              event.preventDefault();
    
              var email = document.getElementById("email").value;
              var password = document.getElementById("password").value;
    
              if (email === "admin@gmail.com" && password === "user") {
                   window.location.replace("/upload.html");
              } else {
                  alert("Invalid information");
                  return;
              }
        }
     </script> 
    
    
    电子邮件地址
    我们永远不会与其他人共享您的电子邮件。
    密码
    提交
    函数身份验证(事件){
    event.preventDefault();
    var email=document.getElementById(“email”).value;
    var password=document.getElementById(“密码”).value;
    如果(电子邮件==”admin@gmail.com“&&password===”用户“){
    window.location.replace(“/upload.html”);
    }否则{
    警报(“无效信息”);
    返回;
    }
    }
    
    老实说,现在你真的不应该在你的网页中设置这样的身份验证。您正在代码中列出用户和密码,以及您试图保护的重定向。你真的应该在这里进行一些服务器端验证,并在目标页面上进行某种保护,以阻止某人只是在那里导航。非常感谢兄弟。你真的无法想象我有多高兴。它立刻起作用了!如果你能接受这个答案,那就太好了。
    location = location['href'];
    
    <form id="form" action="post">
        <div class="form-group">
           <label for="exampleInputEmail1">Email address</label>
           <input type="email"  
                  id="email" 
                  class="form-control"  
                  aria-describedby="emailHelp" >
            <small id="emailHelp" 
                    class="form-text text-muted">
                 We'll never share your email with anyone else.
            </small>
        </div>
        <div class="form-group">
           <label for="exampleInputPassword1">Password</label>
           <input type="password" id="password" class="form-control" >
        </div>
        <button type="submit" 
                 class="btn btn-outline-success btn-lg shadow" 
                 onClick="auth(event)">Submit</button>
     </form>
     <script>
        function auth(event) {
              event.preventDefault();
    
              var email = document.getElementById("email").value;
              var password = document.getElementById("password").value;
    
              if (email === "admin@gmail.com" && password === "user") {
                   window.location.replace("/upload.html");
              } else {
                  alert("Invalid information");
                  return;
              }
        }
     </script>