Implement the ajax pop-up window login function in ECSHOP and ecshopajax pop-up window Login

Source: Internet
Author: User

Implement the ajax pop-up window login function in ECSHOP and ecshopajax pop-up window Login

In the previous article, we introduced how to use the openSpeDiv method to implement the Ecshop logon window. Click here for reference.

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

In ECSHOP, user. PHP has a request for processing user login.

/ * Handle ajax login request * /
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));
  }
  / * Check the verification code * /
  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 (); // Update user information
  recalculate_price (); // Recalculate the price of goods in the shopping cart
  $ 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));
}
Modify the above code and delete the part that needs verification code

Change to

/ * Handle ajax pop-up login request * /
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 (); // Update user information
  recalculate_price (); // Recalculate the price of goods in the shopping cart
  $ 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));
}
will

// Actions that do not require login or verify whether you are logged in (such as ajax processing)
$ 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');
Change to

// Actions that do not require login or verify whether you are logged in (such as ajax processing)
$ 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');
Under the common.js file,

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

// Generate content in the layer
 newDiv.innerHTML = '<form id = "ajax_loginForm"> Username: <br> <input type = "text" name = "username" id = "ajax_username" /> Password: <br> <input type = "password" name = "password" id = "ajax_password" /> <br> <br> <button type = "button" onclick = "ajaxLoginSubmit ()"> Log </ button> <button type = "button" onclick = "closeLoginForm ( ) "> Close </ button> </ form> ';
Write two more methods 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 ('Login successful');
 } else {
  alert ('Login failed');
 }
 return false;
}
The above is the ajax pop-up login function in ECSHOP introduced to you by the editor. I hope it will be helpful to everyone. If you have any questions, please leave a message for me, and the editor will respond to you in time. Thank you very much for your support to the helper home website!

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.