简体   繁体   English

使用ajax提交登录表单

[英]submitting a login form using ajax

I have this javascript in one of my forms. 我在其中一种表单中有此javascript。 This one is the login form I get the URL from the form itself I'm using API style, ("not sure if I got the concept right") but I will attach the code down 这是一个登录表单,我使用API​​样式从表单本身获取URL(“不确定我是否理解正确”),但我会将代码附加到下面

var formup = $('#loginfrom');
formup.submit(function () {
    $.ajax({
        type: formup.attr('method'),
        url: formup.attr('action'),
        data: formup.serialize(),
        success: function (data) {
            alert('you have successfuly logged in');
            window.location = "profile.html";
        }
    });

    return false;
});

here is my login function called from api file 这是我从api文件调用的登录功能

if(!mysql_fetch_array($result)){
return $res;
}
else
    return $resc;

where $res returns "error", and $resc returns "correct" 其中$ res返回“错误”,而$ resc返回“正确”

and my api.php 和我的api.php

if(!isset($function))
        $resp['err'] = "Function: ".$action." does not exist";
    else
        $resp = $function($request);

when I enter the correct user name and password, it works fine and I'm directed to the user profile. 当我输入正确的用户名和密码时,它可以正常工作,并定向到用户配置文件。

However, I don't know how to handle if I got the user name or password incorrect I don't want to be directed to profile 但是,如果我输入的用户名或密码不正确,我不知道如何处理。

I do think the return from php should do something to tell the javascript that the login did not work then do something else 我确实认为从php返回的内容应该做一些事情告诉javascript登录不起作用,然后再做其他事情

please provide me some help regarding this problem, I will appreciate it. 请提供有关此问题的一些帮助,我们将不胜感激。

The request is always a success so, you have to check the value of the data variable to redirect to an error page 该请求总是成功的,因此,您必须检查data变量的值以重定向到错误页面

...
success: function (data) {
  if ( data == 'correct') {
    alert('you have successfuly logged in');
    window.location = "profile.html";
  } else {
     // handle error
  }
}
...

Change your login code to this 将您的登录代码更改为此

if(!mysql_fetch_array($result)){
  echo 'failure'; die;
}
else    {
    echo 'success'; die;
}

And change your ajax success to this 并将您的ajax成功更改为此

success: function (data) {
        if (data == 'success') {
            alert('you have successfuly logged in');
            window.location = "profile.html";
        } else {
             alert('Login Failed');
        }
    }

Use datatype as json to get the return value from php. 使用datatype作为json从php获取返回值。 Check my suggestions in the below code. 在下面的代码中检查我的建议。

$.ajax({
        type: formup.attr('method'),
        url: formup.attr('action'),
        data: formup.serialize(),
        dataType: 'json',
        success: function (data) {

            if(data.error == 0) {
                alert('you have successfuly logged in');
                window.location = "profile.html";
            } else {

                alert("error");
            }
        }
    });

In your php file, convert $res and $resc into an array and then later use json_encode. 在您的php文件中,将$ res和$ resc转换为数组,然后再使用json_encode。 Like this, 像这样,

if(!mysql_fetch_array($result)){
    echo array('value'=>$res,'error'=>0);
}
else
    echo array('value'=>$res,'error'=>1);

You can do something with the data or you can have PHP return a 401 status, if you return something with your PHP instead of a 401 status than you can have your JavaScript check the returned text: 您可以对数据执行某些操作,也可以让PHP返回401状态,如果使用PHP而不是401状态返回某些内容,则可以让JavaScript检查返回的文本:

var formup = $('#loginfrom');
formup.submit(function () {
    $.ajax({
        type: formup.attr('method'),
        url: formup.attr('action'),
        data: formup.serialize(),
        success: function (data) {
            console.log(data);
            // check your console (press F12 in Chrome or Firefox with firebug plugin)
            // do something if the data is valid.
            window.location = "profile.html";
        }
    });

    return false;
});

When the login fails in PHP and you want to return 401 (causing your javascript success function not to be called but you might need a fail/error function in JS. 如果在PHP中登录失败,并且您想返回401(导致不调用javascript成功函数,但是在JS中可能需要使用fail / error函数。

header("HTTP/1.0 401 Unauthorized");

And you need to add a fail callback in your javaScript so the user knows the log in failed: 并且您需要在javaScript中添加一个失败回调,以便用户知道登录失败:

    $.ajax({
        ...
        success: function (data) {
          ...
        },
        error: function(){
           //inform the user
        }
    });

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

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