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

标签 javascript php alert

主页是index.php,它包含一个不同的sidebar_signin_block.php文件,该文件创建用户登录 block 。在 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>";

这是我的问题,需要帮助。在 index.php 页面上,如果登录成功或失败,包含的文件 sidebar_signin_block.php 将打开,并且是 View 中的网页。我想留在主页index.php并且不希望打开sidebar_signin_block.php页面。任何建议将不胜感激。

以防万一您需要它来诊断,我在下面包含了整个 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>

最佳答案

登录表单的目标(action 属性)指向侧边栏文件。因此,当用户按下提交按钮时,浏览器会请求该页面并将其加载到当前框架中。

要避免这种情况,请向表单添加一个 submit 事件处理程序,该处理程序使用 AJAX 将表单数据发送到服务器,然后返回 false。这会抑制加载表单目标的正常浏览器行为。 AJAX 响应应指示登录是成功还是失败,此时您可以根据需要加载启动页面或显示警报。

关于Javascript 警报正在打开 PHP 包含文件并且不停留在主页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001415/

相关文章:

javascript - "this"失去其父级 Core 的范围?

php - Laravel:如果传递了未知字段,如何使更新无效

javascript - 在 Chrome 中,警报在执行弹出窗口之前不会显示 HTML

javascript - 防止用户在 javascript 的文本输入中输入重复的条目

Javascript 获取当前的 Controller 和 Action on Rails

javascript - 检查对象数组中添加和删除了多少元素

java - 将变量存储到我的服务器

php - 用户提交前 5 名并按受欢迎程度排序

php - PHP和MySQL-警告:mysqli_num_rows()期望参数1为mysqli_result,给定 bool 值

java - 警告窗口,直到方法在 Swing 中完成执行