简体   繁体   English

ajax登录表单,重新加载到表单而不是页面上

[英]ajax login form, reloading onto the form rather than page

so I have a login form where I used ajax to catch the login errors ie "incorrect password" from my login.php, and to display them onto the same login form rather than reloading a new page with the error message. 所以我有一个登录表单,在其中我使用ajax从我的login.php捕获登录错误(例如“密码错误”),并将其显示在相同的登录表单上,而不是重新加载带有错误消息的新页面。 That now works fine, but when the login IS successful it now loads the entire logged in page onto the login form rather than loading it to the entire page! 现在可以正常工作,但是登录成功后,它会将整个已登录页面加载到登录表单上,而不是将其加载到整个页面上! You can see in the picture below. 您可以在下图中看到。 login ajax error Here is my code for the ajax: 登录ajax错误这是我的ajax代码:

 $("#login_button").click(function(){

    $.post($("#login_form").attr("action"), $("#login_form :input").serializeArray(), function(info){$("#login_errors").html(info);});
    // Prevent the default action from occurring.
    return false;
});

$("login_form").submit(function(){
    return false;
});

and here is my code for my login form: 这是我登录表单的代码:

<form id= "login_form" action="login.php" method="post">
    <span id="login_errors" style="color:#F00;"></span>
    <label>Email Address</label>
    <input type="text" name="email" id="email" required="required"/>
    <br />

    <label>Password</label>
    <input type="password" name="password" id="password" required="required"/>
    <br />

    <div class="checkbox">
    <input id="remember" type="checkbox" name="keep" />
    <label for="remember">Keep me signed in</label>
    </div>

    <div class="action_btns">
    <div class="one_half last"><input type="submit" class="btn btn-blue" id="login_button" value="Login"></div>
    <div class="one_half last"><a href="#" id="register_form" class="btn">Sign up</a></div>
   </div>
 </form>

Can anyone see what the issue is here? 谁能看到这里的问题吗? Thanks 谢谢

It's because your ajax callback does the same thing every time. 这是因为您的ajax回调每次都会执行相同的操作。 No matter what is returned, it goes into $("#login_errors").html() . 无论返回什么,它都会进入$("#login_errors").html()

What you might want to do is send back a json with a page url if you succeed. 您可能想要做的是,如果成功,则将带有页面URL的json发送回去。 Then you could do something like this: 然后,您可以执行以下操作:

$.post( $("#login_form").attr("action"), 
  $("#login_form :input").serializeArray(), 
  function(info){
    if(info.urlRedirect!=null) 
       window.location.href = info.urlRedirect;
    else $("#login_errors").html(info);
  });

To do this, you would need to modify your php server side component to do something like this: 为此,您需要修改php服务器端组件以执行以下操作:

<?php
$success = $_REQUEST['s'];
if($success) {
    header('Content-type: application/json');
    echo json_encode(array('urlRedirect'=> '/home.php'));
} else {
    echo "<div>wrong username or password - please try again</div>";
}
?>

This is just a sample to show how you return a JSON with a URL if your login succeeds, and to return an html string (which would be your login form) if login fails. 这只是一个示例,显示了登录成功后如何返回带有URL的JSON,以及在登录失败后如何返回html字符串(将是您的登录表单)的示例。

If you do this, and you set a breakpoint inside of your function(info){} in your javascript, you will find that on success, info has a urlRedirect property. 如果执行此操作,并且在javascript中的function(info){}中设置了一个断点,则发现成功时info具有urlRedirect属性。 And on failure, you will find that info is just a regular string with html data. 并且在失败时,您会发现info只是带有html数据的常规字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM