Home > Web Front-end > JS Tutorial > body text

How to use ajax to implement pop-up login

php中世界最好的语言
Release: 2018-04-02 11:42:01
Original
3608 people have browsed it

This time I will show you how to use ajax to realize pop-up window login, and what are the precautions for using ajax to realize pop-up window login. The following is a practical case, let's take a look.

The following describes how to implement AJAX pop-up login.

In user.PHP in ECSHOP, there is a request to handle

user login.

/* 处理 ajax 的登录请求 */ 
elseif ($action == 'signin') 
{ 
 include_once('includes/cls_json.php'); 
 $json = new JSON; 
 $username = !empty($_POST['username']) ? json_str_iconv(trim($_POST['username'])) : ''; 
 $password = !empty($_POST['password']) ? trim($_POST['password']) : ''; 
 $captcha = !empty($_POST['captcha']) ? json_str_iconv(trim($_POST['captcha'])) : ''; 
 $result = array('error' => 0, 'content' => ''); 
 $captcha = intval($_CFG['captcha']); 
 if (($captcha & CAPTCHA_LOGIN) && (!($captcha & CAPTCHA_LOGIN_FAIL) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0) 
 { 
  if (empty($captcha)) 
  { 
   $result['error'] = 1; 
   $result['content'] = $_LANG['invalid_captcha']; 
   die($json->encode($result)); 
  } 
  /* 检查验证码 */ 
  include_once('includes/cls_captcha.php'); 
  $validator = new captcha(); 
  $validator->session_word = 'captcha_login'; 
  if (!$validator->check_word($_POST['captcha'])) 
  { 
   $result['error'] = 1; 
   $result['content'] = $_LANG['invalid_captcha']; 
   die($json->encode($result)); 
  } 
 } 
 if ($user->login($username, $password)) 
 { 
  update_user_info(); //更新用户信息 
  recalculate_price(); // 重新计算购物车中的商品价格 
  $smarty->assign('user_info', get_user_info()); 
  $ucdata = empty($user->ucdata)? "" : $user->ucdata; 
  $result['ucdata'] = $ucdata; 
  $result['content'] = $smarty->fetch('library/member_info.lbi'); 
 } 
 else 
 { 
  $_SESSION['login_fail']++; 
  if ($_SESSION['login_fail'] > 2) 
  { 
   $smarty->assign('enabled_captcha', 1); 
   $result['html'] = $smarty->fetch('library/member_info.lbi'); 
  } 
  $result['error'] = 1; 
  $result['content'] = $_LANG['login_failure']; 
 } 
 die($json->encode($result)); 
}
Copy after login
Modify the above code and delete the part that requires verification code

and change it to

/* 处理 ajax弹窗登录请求 */ 
elseif ($action == 'ajax_login') 
{ 
 include_once('includes/cls_json.php'); 
 $json = new JSON; 
 $username = !empty($_POST['username']) ? json_str_iconv(trim($_POST['username'])) : ''; 
 $password = !empty($_POST['password']) ? trim($_POST['password']) : ''; 
 $result = array('error' => 0, 'content' => ''); 
 $captcha = intval($_CFG['captcha']); 
 if ($user->login($username, $password)) 
 { 
  update_user_info(); //更新用户信息 
  recalculate_price(); // 重新计算购物车中的商品价格 
  $smarty->assign('user_info', get_user_info()); 
  $ucdata = empty($user->ucdata)? "" : $user->ucdata; 
  $result['ucdata'] = $ucdata; 
  $result['content'] = $smarty->fetch('library/member_info.lbi'); 
 } 
 else 
 { 
  $result['error'] = 1; 
  $result['content'] = $_LANG['login_failure']; 
 } 
 die($json->encode($result)); 
}
Copy after login
Change

// 不需要登录的操作或自己验证是否登录(如ajax处理)的act 
$not_login_arr = 
array('login','act_login','register','act_register','act_edit_password','get_password','send_pwd_email','password', 'signin', 'add_tag', 'collect', 'return_to_cart', 'logout', 'email_list', 'validate_email', 'send_hash_mail', 'order_query', 'is_registered', 'check_email','clear_history','qpassword_name', 'get_passwd_question', 'check_answer');
Copy after login
to

// 不需要登录的操作或自己验证是否登录(如ajax处理)的act 
$not_login_arr = 
array('ajax_login','login','act_login','register','act_register','act_edit_password','get_password','send_pwd_email','password', 'signin', 'add_tag', 'collect', 'return_to_cart', 'logout', 'email_list', 'validate_email', 'send_hash_mail', 'order_query', 'is_registered', 'check_email','clear_history','qpassword_name', 'get_passwd_question', 'check_answer');
Copy after login
common.js file,

In the openLginp() method, modify the HTML code of newp.innerHTML and add an ajaxLoginSubmit() method to the login box label.

//生成层内内容 
 newp.innerHTML = '<form id="ajax_loginForm">用户名:<br><input type="text" name="username" id="ajax_username"/>密码:<br><input type="password" name="password" id="ajax_password"/><br><br><button type="button" onclick="ajaxLoginSubmit()">登录</button> <button type="button" onclick="closeLoginForm()">关闭</button></form>';
Copy after login
Just write two more methods by yourself

function ajaxLoginSubmit(){ 
 var username = document.getElementById('ajax_username').value; 
 var password = document.getElementById('ajax_password').value; 
 Ajax.call('user.php?act=ajax_login','username='+username+'&password='+password,ajaxLoginResponse,'POST','JSON'); 
} 
function ajaxLoginResponse(result){ 
 if(result.error == 0){ 
  alert('登录成功'); 
 }else{ 
  alert('登录失败'); 
 } 
 return false; 
}
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of ajax three-level linkage graphic and text (with code)

Detailed explanation of the basics and login steps of Ajax

The above is the detailed content of How to use ajax to implement pop-up login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!