重定向不适用于登录表单中的 ajax 和 PHP。它保留在索引页中

当用户使用电子邮件和密码填写两个表单字段并点击登录按钮时,我的脚本工作正常,但单击登录按钮后,我想将用户重定向到主页,但用户仍保留在索引页面中,当我输入浏览器网址时像 localhost/my_site-name/home.php 这样的栏,系统带我进去,我看到我在该页面中回显的所有会话变量以进行检查,所以我希望当用户单击登录按钮时,用户自动重定向到主页,这里是我的代码


 <form class="form-inline" method="post" action="" id="loginForm">

    <ul class="navbar-nav mr-5">

        <li class="nav-item">

           <input class="form-control mr-sm-2 input-signUp-form js-signInEmail" type="email" name="signInEmail" placeholder="Email"/>

            <input class="form-control mr-sm-2 input-signUp-form js-signInPassword" type="password" name="signInPassword" placeholder="password"/>

           <button  class="btn btn-danger my-2 my-sm-0 js-signInButton" type="submit" name="signInButton">Sign In <i class="fas fa-heart"></i></button>

        </li>

        <p class="ml-2">

            <input type="checkbox" class="form-check-input" id="exampleCheck1"> Remember Me!

            <span><a href="#" class="ml-4 mt-2">Forget Password</a></span>


        </p>

    </ul>

</form>

这是我的脚本代码,我在 jquery 链接 addd 之后将其包含在页面底部


$(document).ready(function(){

    $(".js-signInButton").click(function(){

       var signInemail = $(".js-signInEmail").val();

       var signInPassword = $(".js-signInPassword").val();

       $.ajax({

           url:"signIn.php",

           type:"POST",

           data:{

             "signInEmail":signInemail,

             "signInPassword":signInPassword

           },

           success: function (html) {

               if(html == 'true'){

                    // alert("Successfull Login");

                   window.location.href = "home.php";

               }

               else {

                   alert("Error Login");

               }

           }

       });

    });

});


Smart猫小萌
浏览 119回答 4
4回答

犯罪嫌疑人X

尝试三重等于,因为您得到的响应是字符串而不是布尔值。&nbsp;success: function (html) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(html === 'true'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alert("Successfull Login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.location.href = "home.php";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert("Error Login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

慕田峪9158850

在脚本中使用 e.preventDefault() 。此行不会通过 php 提交您的表单,并且您的脚本将正常运行。尝试这个!$(document).ready(function(){&nbsp; &nbsp; $(".js-signInButton").click(function(e){&nbsp; &nbsp; &nbsp; &nbsp;e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp;var signInemail = $(".js-signInEmail").val();&nbsp; &nbsp; &nbsp; &nbsp;var signInPassword = $(".js-signInPassword").val();&nbsp; &nbsp; &nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url:"signIn.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type:"POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"signInEmail":signInemail,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"signInPassword":signInPassword&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success: function (html) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(html == 'true'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alert("Successfull Login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.location.href = "home.php";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert("Error Login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; });});

HUH函数

在 Ajax 的成功脚本中,尝试这样,看看会发生什么。success:&nbsp;function&nbsp;(html)&nbsp;{ &nbsp;&nbsp;&nbsp;window.location.href&nbsp;=&nbsp;"home.php"; }如果这是重定向到主页,则该语句肯定&nbsp;if(html == 'true'){返回 false。如果它没有重定向到主页,请检查您是否给出了正确的路径。

侃侃尔雅

尝试使用 echo exit;if ($userSignInRows > 0) {&nbsp; &nbsp;// in here i update the user online status form 0 to 1&nbsp; &nbsp;$sqlUpdate = "UPDATE users SET user_online_status = '1' WHERE user_email='$signInEmail'";&nbsp; &nbsp;$updateResult = mysqli_query($conn, $sqlUpdate);&nbsp; &nbsp;// in here i perform this query to select user online status after updating it&nbsp; &nbsp;$select = "SELECT * FROM users WHERE user_email ='$signInEmail'";&nbsp; &nbsp;$selectResult = mysqli_query($conn, $select);&nbsp; &nbsp;$updateArray = mysqli_fetch_array($selectResult);&nbsp; &nbsp;$_SESSION['onlineStatus'] = $updateArray['user_online_status'];&nbsp; &nbsp;$_SESSION['signInId'] = $userSignInArray['user_id'];&nbsp; &nbsp;$_SESSION['signInEmail'] = $userSignInArray['user_email'];&nbsp; &nbsp;$_SESSION['signInName'] = $userSignInArray['user_full_name'];&nbsp; &nbsp;echo "true";&nbsp; &nbsp;echo exit;}或者你可以回显1;回显退出;在脚本中只需检查if(html){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; window.location.href = "home.php";&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP