簡體   English   中英

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

[英]How to redirect after successful php if statement in ajax?

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:

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

*更新 像下面提到的@Patrick Q 那樣回顯數據似乎沒有什么不好。 您能否嘗試修剪在 javascript/jquery 中收到的數據以檢查意外的空格

data.trim()

如果要添加更多變量,可以執行以下解決方案。 (或者如果你喜歡它)

您不應回顯 ajax 的結果。 相反,您應該將其返回為 json

PHP 文件:

$ajax_result = array():
$ajax_result['success'] = false;
$ajax_result['message'] = 'Incorrect login data';

if(something)
{
    $ajax_result['success'] = 'login_success';
    $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 等選擇變量來操作它。

jQuery/Javascript:

     $.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.message);  
            setTimeout(function(){  
                 $('#login_response').fadeOut("slow");  
            }, 7000);

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

這是最基本的用途。 您可以將其修改為僅可通過 ajax 查詢等進行訪問。

感謝上面的所有答案和評論。 老實說,我喜歡 Patrick Q Idea,這就是我到目前為止所取得的成就。

Ajax

  success:function(data){  
      if(data != null && data == "success"){ //redirect...
               window.location = "http://google.com";
    } else { //report failure...

                   $('form').trigger("reset"); 
                   $('#login_response').fadeIn().html(data);
                   setTimeout(function(){  
                   $('#login_response').fadeOut("slow");  
                   }, 7000);
              }
    }  

PHP

          if($num > 0) 
            {
              //if record found
                $_SESSION["loggedin"] = TRUE;
                $_SESSION['cid']=$num['cid'];

                echo "success";

            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM