dongtai419309 2019-02-04 22:33
浏览 184
已采纳

如何在php和JavaScript中重定向php Window.location.href或Window.open()方法

I follow up a tutorial to learn more about php, in it's source code there is something which seems works at that time but not anymore. here is the code , please let me know what should i change in the code in order to make login process work (currently after entering a valid user name and pass and clicking login it freezes and show first page and not go to home.php

here is template/header.php:

<div class="container">
        <!--Head wrap starts-->
        <div id="head_wrap">
            <!--Header starts-->
            <div id="header">
                <img src="images/logo.png" style="float:left;"/>
                <form method="post" action="" id="form1">
                    <strong>Email:</strong>
                    <input type="email" id="email" name="email" placeholder="Email" required="required" />
                    <strong>Password:</strong>
                    <input type="password" id="pass" name="pass" placeholder="****" required="required"/>
                    <button type="submit" id="login">Login</button>
                </form>
            </div>
            <!--Header ends-->
        </div>

here is login.php

<?php
        session_start();

        include("includes/connection.php");

        if(isset($_POST['login'])){

        $email = mysqli_real_escape_string($con,$_POST['email']);
        $pass = mysqli_real_escape_string($con,$_POST['pass']);

        $get_user = "select * from users where user_email='$email' AND user_pass='$pass'";

        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if($check==1){
                $email = mysqli_real_escape_string($con,$_POST['email']);

            $_SESSION['user_email']=$email;

                        echo "<script>window.open('home.php','_self')</script>";



        }
            else {
            echo "<script>alert('Passowrd or email is not correct!')</script>";
        }

        }
?>

please note i have tried

        echo "<script> window.location.href = 'home.php';</script>";

instead of

        echo "<script>window.open('home.php','_self')</script>";

and still doesn't work, since it's tutorial and i have search through stackoverflow can't find any answer i appreciate your help.

  • 写回答

4条回答 默认 最新

  • duanlipeng4136 2019-02-05 00:48
    关注

    This is your HTML code but with a submit button. You say all files are located in the same folder so this should work. I did not make any changes to login.php but it should run when the page is submitted.

    <div class="container">
     <!--Head wrap starts-->
     <div id="head_wrap">
      <!--Header starts-->
      <div id="header">
       <img src="images/logo.png" style="float:left;"/>
       <form method="post" action="login.php" id="form1">
        <strong>Email:</strong>
        <input type="email" id="email" name="email" placeholder="Email" required="required" />
        <strong>Password:</strong>
        <input type="password" id="pass" name="pass" placeholder="****" required="required"/>
        <input type="submit" id="login" name="login" value="Login">
       </form>
      </div>
      <!--Header ends-->
     </div>
    </div>
    

    Edit: I can't debug your entire project but after looking over some things I see you are not using the 'name' attribute. When a page is submitted a name/value pair is sent in the $_POST array. If you have no 'name' attribute nothing is sent. Start by adding the 'name' attribute. I have modified the above HTML code to show you how.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名