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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题