How does an AJAX user log in and jump to a page? Explanation of the Ajax user jump page (with instance)

Source: Internet
Author: User

This article mainly introduces the AJAX user login after the situation, the following detailed code examples, the need for friends to quickly look over. Start the introduction to this article now

Recently in the study of the use of TP framework, in the background management system to do a background login page, want to prompt users in real-time input information return situation

Of course, the same user name password error to be verified in the background and not refreshed in the case of display on the page, then the first thought of asynchronous processing (this is the door art)

$this_ajaxreturn () on TP, the method can return the information to the previous paragraph, and the corresponding information is refreshed locally with the JQ judgment.

But when I actually entered the correct username password, I couldn't jump to the admin page and just return a JSON data stream

The original use of asynchronous in the back of the backend to return data, to the JS code processing, return to the JSON stream, then the background redirection into the stream, and will not refresh the page, can only be received by the corresponding see page information. (want to see more on the Topic.alibabacloud.comAJAX Development Manual section of the study)

Then we know that in JS processing so you can use JS redirect windows.location.href= ' url '; jump to a specific page

JS Code

$ (function () {$ (' button '). Click (function () {var user = $ (' input[name=user] ');        var pwd = $ (' input[name=pwd] ');        var verify = $ (' input[name=verify] ');            if (user.val () = = "| | pwd.val () = =") {$ (' [Name=user] '). focus ();            $ (' #errormsg '). HTML ("<strong> user name or password cannot be null <strong>");        return false;            } else if (verify.val () = = ') {$ (' #errormsg '). HTML ("<strong> verification code cannot be null <strong>");        return false; } else{$.ajax ({url:handle, data:{' user ': User.val (), ' pwd ':p wd.val (), ' Verify ': Verify.val ()}, type: "POST", DataType: ' JSON ', success                             : function (data) {if (Data.status = = ' 1 ') {window.location.href = Dr;                           } else if (data.status = = ' 2 ') {    $ (' #errormsg '). HTML ("<strong> captcha error <strong>"); }else if (data.status = = ' 0 ') {$ (' #errormsg '). HTML ("<strong> username or password error!)                            <strong> "); }}, Error:function (data) {alert ("Error:" + data                        . code);            }   }); }    });})

Background Code

$verify = I (' Verify ', ' ', ' MD5 ');                if ($verify!== $_session[' verify ') {$this->ajaxreturn (' status ' = ' 2 ');            Die ();        } $username =i (' User ', ', ');        $passward =i (' pwd ', ', ');        $date =m (' admin ', NULL);        $date->where (Array (' username ' = $username))->find ();            if ($date and $date->passward = = $passward) {$id = $date->id;            $login _time = time ();            $login _ip = Get_client_ip ();            $date = array (' id ' = = $id, ' login_ip ' = + $login _ip, ' login_time ' = $login _time);            M (' admin ')->save ($date);            Session (' UID ', $date [' id ']);            Session (' IP ', $date [' login_ip ']);            $this->ajaxreturn (Array (' status ' = ' 1 '));        $this->success (' Landing success ', U (' Admin/admin/index '));            } else {$this->ajaxreturn (' status ' = ' 0 '); $this->redirect (' Admin/index/index '); }    }

the above also has to accept the background data to determine whether the user name password is correct process

This is the end of this article (want to see more on the Topic.alibabacloud.comAJAX User manual section of the study), there are questions can be in the message below the question.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.