dongtai419309
dongtai419309
2019-02-04 22:33
浏览 95

如何在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
    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.

    点赞 评论
  • dongshang4984
    dongshang4984 2019-02-04 22:42

    You have to use header(...) function but don't forget that your page keep to run at the end. Don't forget to use with die to stop your script. ;)

    die(header("Location: home.php"))
    

    or after 5 seconds :

    header("refresh: 5; url=home.php");
    
    点赞 评论
  • doutuo4285
    doutuo4285 2019-02-05 05:22
     if($check==1){
                $email = mysqli_real_escape_string($con,$_POST['email']);
    
            $_SESSION['user_email']=$email;
    
                        return 1;
       }
            else {
             return 0;
        }
    

    and javascript check status 1 and 0 then window.location.href and window.open use

    点赞 评论
  • duanquannan0593
    duanquannan0593 2019-02-05 05:53

    Check in your file..

    1) header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

    2) Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.

    3) after header('location: home.php'); add exit();

    4) after sesssion_start() add ob_start();

    点赞 评论

相关推荐