温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ECSHOP中如何实现ajax弹窗登录功能

发布时间:2021-07-06 14:55:51 来源:亿速云 阅读:108 作者:小新 栏目:web开发

这篇文章将为大家详细讲解有关ECSHOP中如何实现ajax弹窗登录功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

在ECSHOP中的user.PHP中有处理用户登录的请求。

/* 处理 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)); 
}

把上面这段代码修改一下,删掉需要验证码的部分

改成

/* 处理 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)); 
}

// 不需要登录的操作或自己验证是否登录(如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');

改成

// 不需要登录的操作或自己验证是否登录(如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');

common.js文件下,

在openLginDiv()方法里,将newDiv.innerHTML的HTML代码修改下,在登录框标签里加个ajaxLoginSubmit()方法。

//生成层内内容 
 newDiv.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>';

再自己写两个方法即可

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; 
}

关于“ECSHOP中如何实现ajax弹窗登录功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI