简体   繁体   English

Javascript警报正在打开一个PHP包含文件,并且没有停留在主页上

[英]Javascript alert is opening a PHP include file and not staying on the home page

The home page is index.php and it contains an include for a different sidebar_signin_block.php file that creates a user login block. 主页是index.php,它包含一个用于创建用户登录块的不同sidebar_signin_block.php文件的包含。 In the sidebar_signin_block.php if the login succeeds I am using this javascript to send them to a login success splash page: echo "<script>window.open('login-success.php','_self')</script>";} Now on the other hand if the login fails I have an else statement with a javascript for an alert box that looks like this: echo "<script>alert('Email or password is incorrect please try again')</script>"; 在sidebar_signin_block.php中,如果登录成功,我正在使用此javascript将它们发送到登录成功的初始页面: echo "<script>window.open('login-success.php','_self')</script>";}现在,另一方面,如果登录失败,我将使用带有JavaScript的else语句创建一个警告框,如下所示: echo "<script>alert('Email or password is incorrect please try again')</script>";

Here is my problem that I need help with. 这是我需要帮助的问题。 While at the index.php page if the login succeeds or if it fails the included file sidebar_signin_block.php opens up and is the web page in view. 在index.php页面上,如果登录成功或失败,则包含的文件sidebar_signin_block.php将打开并显示在网页中。 I want to stay on the home page index.php and do not want the sidebar_signin_block.php page to open. 我想保留在主页index.php上,并且不想打开sidebar_signin_block.php页面。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Just in case you need it for diagnosing I am including the entire sidebar-signin-block.php file below: 万一您需要它来进行诊断,我将在下面包括整个sidebar-signin-block.php文件:

<?php
session_start();
?>

<html>
<body>
<form method="post" action="sidebar-signin-block.php">

        <table width="90%" border="0" align="center" bgcolor="white">


            <tr>
                <td bgcolor="ffffff" colspan="2" align="center"><h2>User Login</h2></td>
            </tr>

            <tr>
                <td align="right">Email:</td>
                <td><input type="text" name="email"></td>
            </tr>

            <tr>
                <td align="right">Password:</td>
                <td><input type="password" name="password"></td>
            </tr>

            <tr>
                <td colspan="2" align="center"><input type="submit" name="login" value="Login"></td>

            </tr>

            <tr>
                <td colspan="2" align="center"><h3 style="margin-top:7px;"><a href="nonadmin_user_forgot_password.php" target="_blank" title="Reset Your Lost Password">Forgot Password?</a></h3></td>
            </tr>


            <tr>
                <td bgcolor="#ffffff" colspan="2" align="center"><div style="padding-top:5px;"><span style="font-size:20px;">Don't have an account?<br /><a href="/includes/register-user.php" title="Register with us!" target="_self">Sign Up</a> is <em>quick</em> and <em>easy</em>!</span></div></td>

        </table>
    </form>

    <?php 

// Connecting to the database and making the Bcrypt functions available
include("admin/includes/connect.php");
include ("lib/password.php");

// Gathering and sanitizing user login input
if(isset($_POST['login'])){

    $email = trim(((isset($conn) && is_object($conn)) ? mysqli_real_escape_string($conn, $_POST['email']) :((trigger_error  ("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : "")));

        $pass = trim(((isset($conn) && is_object($conn)) ? mysqli_real_escape_string($conn, $_POST['password']) : ((trigger_error   ("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : "")));

         // Checking the database records for the user login input
        $hash_query = "select nonadmin_user_pass from nonadmin_user_login where email='$email'";{
                $run_query = mysqli_query($conn, $hash_query);}
                while ($row = mysqli_fetch_assoc($run_query)) {
                $fetch_pass = $row['nonadmin_user_pass'];
                    }
        // If the user email and password matches we start a session                               
        if ((password_verify($pass, $fetch_pass)) == 1){

              // Verifying user login success with splash page then sending user back to the home page
              $_SESSION['email']=$email;
              echo "<script>window.open('login-success.php','_self')</script>";}
    // When the user login fails an alert is given to inform them
    else {
    echo "<script>alert('Email or password is incorrect please try again')</script>";
    echo "<script>window.open('index.php','_self')</script>";}
}
?>
</body>
</html>

How about check wheter the user is already login or not in index.php 如何检查用户是否已经在index.php中登录

    <?php
    session_start();
    //in sidebar position, do checking
    if(isset($_SESSION['email']) && $_SESSION['email'] != '')
    {
        //if user already login succesfully, do anything here
    } else {
        //if user not yet login, display side bar
        include 'sidebar-signin-block.php
    }
    //your next index.php code

The login form's target ( action attribute) points back to the sidebar file. 登录表单的目标( action属性)指向边栏文件。 Thus when the user presses the submit button, the browser requests that page and loads it into the current frame. 因此,当用户按下“提交”按钮时,浏览器会请求该页面并将其加载到当前框架中。

To avoid this, add a submit event handler to the form which sends the form data to the server using AJAX and then returns false . 为了避免这种情况,请submit表单中添加一个submit事件处理程序,该处理程序使用AJAX将表单数据发送到服务器,然后返回false This suppresses the normal browser behavior of loading the form's target. 这样可以抑制浏览器加载表单目标的正常行为。 The AJAX response should indicate whether the login succeeded or failed, at which point you can load the splash page or show the alert, as appropriate. AJAX响应应指示登录成功还是失败,此时您可以根据需要加载启动页面或显示警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM