SimpleModal PHP 登录表单:如何在成功/失败时重定向

发布于 2024-09-17 17:40:04 字数 688 浏览 2 评论 0原文

希望你能帮忙。这里有一点 jQuery/AJAX 新手。

我有一个基本的 SimpleModal 登录表单,可以从我的网站主页调用它。

login.js 调用 login.php 脚本来检查我的数据库中的用户凭据。

如果登录成功,我会设置一些会话变量,并且通常使用 header 命令将用户发送到他们的个人页面(或登录失败时的错误页面)。

例如。 header("位置:restricted/index.php");

我无法关闭模式登录表单并将父主页重定向到会员页面。 我通常会留下空白的模态方块,上面写着“谢谢”,

如果我尝试用模态关闭来关闭模态表单,

success: function (data) {
 $('#login-container .login-loading').fadeOut(200, function () {
  $('#login-container .login-title').html('Thank you!');
  msg.html(data).fadeIn(200);
  $.modal.close(); //have inserted this line
 });
},

但我会留在主页上。 (用户在此阶段已登录,但尚未发生重定向)。

我已经研究这个问题一两天了。我查看了 WordPress 登录插件,但我一无所知。有什么想法吗?

Hope you can help. A bit of a jQuery/AJAX newb here.

I have a basic SimpleModal login form that may be called from the homepage of my site.

login.js calls the login.php script that checks my database for a user's credentials.

If the login is successful I set a few session variables and I usually use the header command to send the user to their personal page (or the error page on login failure).

eg. header("Location:restricted/index.php");

I can't get the modal login form to close and the parent homepage to redirect to the member page. I'm usually left with the blank modal square saying "Thank You"

If i try to close the modal form with

success: function (data) {
 $('#login-container .login-loading').fadeOut(200, function () {
  $('#login-container .login-title').html('Thank you!');
  msg.html(data).fadeIn(200);
  $.modal.close(); //have inserted this line
 });
},

the modal closes but I am left on the homepage. (The user is logged in at this stage but the redirect has not happened).

I've been kicking this around for a day or 2. I had a look at the wordpress login plugin but I am none the wiser. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-09-24 17:40:04

实际上,重定向可能确实发生了,它只是发生在AJAX 请求内。当您进行重定向时,ajax 函数使用的 XmlHttpRequest 会透明地跟随它,因此返回到客户端的页面是 restricted/index.php。我倾向于做的是在那个页面上添加一个标题,如下所示:

header("Redirect:restricted/index.php");

然后使用 $.ajaxSetup()

$.ajaxSetup({
  complete: function (xhr) {
    var redirect = xhr.getResponseHeader('Redirect');
    if(redirect) window.location.href = redirect;
  }
});

这样(除非您覆盖 complete,否则此选项会保持打开状态)返回的请求到达 restricted/index.php(记住 XmlHttpRequest 实际上去了那里),它将获得一个标头,让客户端知道它需要重定向。

Actually the redirect probably did happen, it just happened inside the AJAX request. When you do a redirect, the XmlHttpRequest that the ajax functions use transparently follows it, so then page that came back to the client is the restricted/index.php. what I tend to do is add a header on that page, something like this:

header("Redirect:restricted/index.php");

Then look for the header, something like this using $.ajaxSetup():

$.ajaxSetup({
  complete: function (xhr) {
    var redirect = xhr.getResponseHeader('Redirect');
    if(redirect) window.location.href = redirect;
  }
});

This way (unless you override complete, it leaves this option open) when a request comes back that made it to the restricted/index.php (remember the XmlHttpRequest actually went there) it'll get a header to let the client know it needs to redirect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文