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

I follow up a tutorial to learn more about php, in it's source code there is something which seems

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.

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.

Share Improve this question edited Feb 5, 2019 at 12:39 Jack asked Feb 4, 2019 at 22:33 JackJack 1291 gold badge1 silver badge9 bronze badges 8
  • Where is the submit button? I only see a normal button. Until you submit the page PHP will not run at all. And you should use the PHP 'header' mand to redirect the page, not Javascript. – CharlesEF Commented Feb 5, 2019 at 0:29
  • @CharlesEF i have tried submit button but that one is not only the bug it has , can you please let me know what other bug the tutorial has – Jack Commented Feb 5, 2019 at 0:56
  • I tried to download the file but I can't get past the social media part. I'm asked to download the file using Facebook, Twitter or Goggle+. Since I don't do any social media at all I'm not going to sign up just to download your file. If you can place the file on another server maybe I can download it, just not from the current location. – CharlesEF Commented Feb 5, 2019 at 1:08
  • You are using a web server aren't you? PHP only runs server side so you need to have a web server working. And PHP must be installed on the web server. – CharlesEF Commented Feb 5, 2019 at 1:12
  • I don't use sites like that so I don't have one to remend to you, sorry. If you can find another site just post the address in a ment. – CharlesEF Commented Feb 5, 2019 at 1:24
 |  Show 3 more ments

4 Answers 4

Reset to default 1

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 :

die(header("refresh: 5; url=home.php"));

Also, for header function works, you should have no html output before.

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.

 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

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744309695a4567889.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信