dongyang0005 2015-03-12 03:34
浏览 56
已采纳

Javascript警报正在打开PHP包含文件而不是停留在主页上

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. 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>";

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. I want to stay on the home page index.php and do not want the sidebar_signin_block.php page to open. 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:

<?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>
  • 写回答

2条回答 默认 最新

  • doudun5009 2015-03-12 04:37
    关注

    The login form's target (action attribute) points back to the sidebar file. 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. 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.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助