This article mainly introduces the situation of jump after ajax user logs in. There are detailed code examples below. Friends in need should quickly take a look. Let’s start the introduction of this article now
I have been learning the use of TP framework recently. When I was working on the backend management system, I made a backend login page. I wanted to prompt the user in real time about the return status of the input information.
Of course, judging that the username and password are incorrect must go through background verification and be displayed on the page without refreshing, so the first thing to think about is asynchronous processing (this is the door Art)
The $this_ajaxReturn(); method on TP can return the information to the previous section, and use jq to judge the local refresh of the corresponding information.
But when I actually enter the correct username and password, I cannot jump to the background management page and just return a json data stream
It turns out that when asynchronous is used to receive the data returned from the background, it is handed over to the js code for processing and the json stream is returned. Then the background redirection enters the stream instead of The page will be refreshed, and the page information can only be seen through the received response. (If you want to see more, go to the PHP Chinese website AJAX Development Manual column to learn)
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>用户名或密码不能为空<strong>"); return false; } else if(verify.val() == '') { $('#errormsg').html("<strong>验证码不能为空<strong>"); return false; } else{ $.ajax({ url: handle, data:{'user':user.val(),'pwd':pwd.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>验证码错误<strong>"); }else if (data.status == '0') { $('#errormsg').html("<strong>用户名或密码错误!<strong>"); } }, error : function(data) { alert("出错:" + data.code); } }); } }); })
Backend code
$verify = I('verify','','md5'); if($verify !== $_SESSION['verify']) { $this->ajaxReturn(array('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('登陆成功',U('Admin/Admin/index')); } else { $this->ajaxReturn(array('status' =>'0')); $this->redirect('Admin/Index/index'); } }
The above also has the process of accepting background data to determine whether the username and password are correct
This article ends here (if you want to see more, go to the PHP Chinese website Learn in the column of the AJAX User Manual). If you have any questions, you can leave a message below.
The above is the detailed content of How do AJAX users jump to the page after logging in? Explanation of ajax user jump page (with examples). For more information, please follow other related articles on the PHP Chinese website!