duandaishi9268 2015-03-22 16:15
浏览 49
已采纳

如何在成功的ajax调用上重定向用户? 否则在表单中显示错误消息

Updated: Thanks for reading - My login form calls on login.php to check whether the user has entered a registered email address or not. If the address is registered, it echoes back "redirect", if it is not registered, it echoes "Email not registered". My current code only redirects to mydomain.com/# since the form action is set to #, because I'm using the ajax to submit the form.

How do I:

Redirect the user to page private.php if the php has echoed "redirect" - otherwise how do I display "Email not registered" within the form, if it echoes "Email not registered"? My login form contains a div to display the error if necessary.

ajax:

 <script>
    $(document).ready(function() {

$("#loginform").submit(function(e){
    e.preventDefault();
    $.post('login/login.php', {email: $('#email').val(), loginsubmit: 'yes'}, function(data)
    {
    if (data === "redirect") 
{ 
window.location = "http://www.gathercat.com/login/private.php"; 
} 

else {  
 $("#formResponse").html(data).fadeIn('100');
        $('#email').val(''); 
    }
, 'text');
    return false;
    }
});
});
</script>

PHP:

...
    if($login_ok) 
            { 
               echo 'redirect';
            } 
            else 
            { 
                echo 'That email address is not registered';            
            } 
...

login form:

...
 <form id="loginform" name="loginform" method="POST" class="login" action="#">
    <input name="email" id="email" type="email" class="feedback-input" placeholder="My Email" required/>
     <div id="formResponse" style="display: none;"></div>
     <button type="submit" name="loginsubmit" class="loginbutton">Login</button>
...

Full PHP

<?php
$emailaddress = $_POST["email"];
?>
<?php 

    // First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // This if statement checks to determine whether the login form has been submitted 
    // If it has, then the login code is run, otherwise the form is displayed 
    if(!empty($_POST)) 
    { 
        // This query retrieves the user's information from the database using 
        // their email. 
        $query = " 
            SELECT 
                email 
            FROM users 
            WHERE 
                email = :email 
        "; 

        // The parameter values 
        $query_params = array( 
            ':email' => $emailaddress
        );   
        try 
        { 
            // Execute the query against the database 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        {    
            die("Failed to run query"); 
        } 
        // This variable tells us whether the user has successfully logged in or not. 
        // We initialize it to false, assuming they have not. 
        // If we determine that they have entered the right details, then we switch it to true. 
        $login_ok = false; 

        // Retrieve the user data from the database.  If $row is false, then the email
        // they entered is not registered. 

         $row = $stmt->fetch(); 
         if($row) { 
         $login_ok = true; 
         }  
        // If the user logged in successfully, then we send them to the private members-only page 
        // Otherwise, we display a login failed message and show the login form again 
        if($login_ok) 
        { 

            // This stores the user's data into the session at the index 'user'. 
            // We will check this index on the private members-only page to determine whether 
            // or not the user is logged in.  We can also use it to retrieve 
            // the user's details. 
            $_SESSION['user'] = $row; 

            // Redirect the user to the private members-only page. 
           echo 'redirect';
        } 
        else 
        { 
            // Tell the user they failed 
            echo 'That email address is not registered';            
        } 
    } 

?>
  • 写回答

3条回答 默认 最新

  • douhang1913 2015-03-22 17:23
    关注

    I've rewritten the code for you as it likes like you had quite a few problems.

     $(document).ready(function() {
    
        $("#loginform").submit(function(e){
            e.preventDefault();
    
            jQuery.ajax({
                    type: "POST",
                    url: "login/login.php",
                    dataType:"text",
                    data: { email: $('#email').val(), loginsubmit: 'yes' },
                    success:function(response){
    
                        if(response == "redirect"){
                            window.location.replace("http://www.gathercat.com/login/private.php"); 
                        }
                        else{
                            $("#formResponse").html(response).fadeIn('100');
                            $('#email').val('');   
                        }
                    }
                })
        });
    });
    

    This is untested but please let me know if you have any questions about how it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示