如何在 ajax 中成功的 php if 语句后重定向?

PHP 正如您在下面的 PHP 代码中看到的那样,我正在尝试重定向用户,但我在 jquery 中很难做到这一点。我想要的是如果登录语句成功login_success那么用户将重定向到 myprofile.php 但什么也没有发生。


    if($query)

          {

             $num=mysqli_fetch_array($query); //fetching all matching records


                if($num > 0) 

                {

                  //if record found

                    $_SESSION["loggedin"] = TRUE;

                    $_SESSION['cid']=$num['cid'];


                    echo  "login_success";


                }

                else

                {

                    echo "invalid email or password.";

                }


           }else

           {

             echo "something went wrong!";

           }

阿贾克斯:


$.ajax({  

                       url:"sql/login_process.php",  

                       method:"POST",  

                       data:$('#login_form').serialize(),  

                       beforeSend:function(){  

                            $('#login_response').html('<span class="text-info"><i class="fas fa-spinner"></i> Loading response...</span>');  

                       },  

                       success:function(data){  

                            $('form').trigger("reset");  

                            $('#login_response').fadeIn().html(data);  

                            setTimeout(function(){  

                                 $('#login_response').fadeOut("slow");  

                            }, 7000);


                            if(data == "login_success") location.href = "http://www.example.com/myprofile.php";

                       }  

                  }); 

我想我在这里遗漏了一些东西。


if(data == "login_success") location.href = "http://www.example.com/myprofile.php";


慕桂英4014372
浏览 89回答 2
2回答

慕运维8079593

*更新下面提到的@Patrick Q 似乎可以回显数据。您能否尝试修剪在 javascript/jquery 中收到的数据以检查意外的空格data.trim()如果您想添加更多变量,您可以执行以下解决方案。(或者如果你喜欢的话)您不应该回显 ajax 的结果。相反,您应该将其作为 json 返回PHP文件:$ajax_result = array():$ajax_result['success'] = false;$ajax_result['message'] = 'Incorrect login data';if(something){&nbsp; &nbsp; $ajax_result['success'] = 'login_success';&nbsp; &nbsp; $ajax_result['message'] = 'You were logged in. You will be redirected now.';}header('Content-type:application/json;charset=utf-8');echo json_encode($ajax_result);这会将结果作为数组返回到前端,您可以通过选择带有 data.success 或 data.message 等的变量来对其进行操作。查询/Javascript:&nbsp; &nbsp; &nbsp;$.ajax({&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;url:"sql/login_process.php",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;method:"POST",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;data:$('#login_form').serialize(),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;beforeSend:function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#login_response').html('<span class="text-info"><i class="fas fa-spinner"></i> Loading response...</span>');&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;},&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;success:function(data){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('form').trigger("reset");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#login_response').fadeIn().html(data.message);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#login_response').fadeOut("slow");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 7000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data.success == "login_success") location.href = "http://www.example.com/myprofile.php";&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp;&nbsp; });&nbsp;这是最基本的用法。您可以修改它以保护只能从 ajax 查询等访问。

米脂

老实说,我喜欢这里的 Patrick Q Idea,这是我到目前为止所取得的成就。阿贾克斯&nbsp; success:function(data){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; if(data != null && data == "success"){ //redirect...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.location = "http://google.com";&nbsp; &nbsp; } else { //report failure...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('form').trigger("reset");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#login_response').fadeIn().html(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setTimeout(function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#login_response').fadeOut("slow");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}, 7000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp;PHP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($num > 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if record found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION["loggedin"] = TRUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['cid']=$num['cid'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "success";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP