javascriptphphtmlwindow.openwindow.location

How redirect php Window.location.href or Window.open () methods in php and JavaScript


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.


Solution

  • 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.