Jump to content

Ajax регистрация


Recommended Posts

Здравствуйте.

пытаюсь сделать регистрацию Ajax на сайте edaoren.ru

 

 $msg = '';
$email = Tools::getValue('email_create_ajax');
$passwd = trim(Tools::getValue('passwd_ajax'));
$firstname =  Tools::getValue('customer_firstname');
$lastname = Tools::getValue('customer_lastname');
$phone = Tools::getValue('phone_mobile');
$gender = Tools::getValue('id_gender');

if(empty($firstname) || (!Validate::isName($firstname) && !empty($firstname)))
 $msg = Tools::displayError('Enter Your Firstname');
elseif(!Validate::isName($lastname) && !empty($lastname))
 $msg = Tools::displayError('Enter Your Lastname');
elseif (empty($email))
 $msg = Tools::displayError('E-mail address is required');
elseif (!Validate::isEmail($email))
 $msg = Tools::displayError('Invalid e-mail address');
elseif (Customer::customerExists($email,false,false))
 $msg = Tools::displayError('An account using this email address has already been registered.');
elseif (empty($passwd))
 $msg = Tools::displayError('Password is required');
elseif (Tools::strlen($passwd) > 32)
 $msg = Tools::displayError('Password is too long');
elseif (!Validate::isPhoneNumber($phone))
 $msg = Tools::displayError('Invalid phone mobile');
elseif (Tools::strlen($phone) != 11)
 $msg = Tools::displayError('Size phone mobile  must be equal 11 numbers');
else {
//  $passwd = md5(_COOKIE_KEY_.$passwd);
 $customer = new Customer();
 $customer->id_gender = $gender;
//  $a = Tools::encrypt($passwd);
 if (Customer::customerExists($email))
  $msg = Tools::displayError('An account using this email address has already been registered. Please enter a valid password or request a new one. ');
//  elseif(!Validate::isBirthDate($customer->birthday))
//   $msg = Tools::displayError('Invalid date of birth.');
 else {
  if (Tools::getValue('newsletter'))
  {
   $customer->newsletter = (int)Tools::getValue('newsletter');
   $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr());
   $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s'));
   if ($module_newsletter = Module::getInstanceByName('blocknewsletter'))
   if ($module_newsletter->active)
 $module_newsletter->confirmSubscription($email);
  } else {
   $customer->newsletter = 0;
   $customer->optin = 0;
  }
  if (Tools::getValue('optin'))
   $customer->optin = (int)Tools::getValue('optin');
  $customer->firstname = $firstname;
  $customer->lastname = empty($lastname) ? ' ' : $lastname;
//   $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);
  $customer->passwd = $passwd;
  $customer->email = $email;
//   $customer->is_guest = Tools::getValue('is_new_customer', 1); //(Tools::isSubmit('is_new_customer') ? !Tools::getValue('is_new_customer', 1) : 0);
  $customer->is_guest = Tools::getValue('is_new_customer', 0); //(Tools::isSubmit('is_new_customer') ? !Tools::getValue('is_new_customer', 1) : 0);
  $customer->active = 1;
  $customer->cleanGroups();
  if (!$customer->is_guest) {
   // we add the guest customer in the default customer group
   $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));
  } else {
   // we add the guest customer in the guest customer group
   $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
  }
  if (!Mail::Send($cookie->id_lang,
 'account',
 Mail::l('Welcome!'),
 array('{firstname}' => $customer->firstname,
  '{lastname}' => $customer->lastname,
  '{email}' => $customer->email,
  '{passwd}' => $customer->passwd
 ),
 $customer->email,
 $customer->firstname.' '.$customer->lastname
   ))
   $msg = Tools::displayError('The email cannot be sent.');
  else {
   $_POST['email'] = $email;
   //if (!Tools::getValue('is_new_customer', 1))
   $_POST['passwd'] = $customer->passwd;//md5(time()._COOKIE_KEY_);
   $_POST['lastname'] = $customer->lastname;
   $_POST['firstname'] = $customer->firstname;
   if (!$customer->add())
 $msg = Tools::displayError('An error occurred while creating your account..');
   else {
 $cookie->id_customer = (int)($customer->id);
 $cookie->customer_lastname = $customer->lastname;
 $cookie->customer_firstname = $customer->firstname;
 $cookie->logged = 0;
 $customer->logged = 0;
 $cookie->passwd = $customer->passwd;
 $cookie->email = $customer->email;
//	 if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE'))
//	  $cookie->account_created = 1;
//	 $cookie->is_guest = !Tools::getValue('is_new_customer', 1);
//	 $cart->secure_key = $customer->secure_key;
 if (Configuration::get('PS_CART_FOLLOWING') AND (empty($cookie->id_cart) OR Cart::getNbProducts($cookie->id_cart) == 0))
  $cookie->id_cart = (int)(Cart::lastNoneOrderedCart((int)($customer->id)));
//	 $id_address = (int)(Address::getFirstCustomerAddressId((int)($customer->id)));
//	 $cookie->id_address_delivery = $id_address;
//	 $cookie->id_address_invoice = $id_address;

//	 Hook::exec('actionCustomerAccountAdd', array(
//	   '_POST' => $_POST,
//	   'newCustomer' => $customer
//	  ));
 $msg = "success";
   }
  }
 }

 

 

подскажите пожалуйста, как сделать так, чтобы после регистрации, сразу прошла регистрация :( у меня выдает fatal error. приходится удалять куки и заного авторизовываться потом.

или подскажите как сделать с существующим authorize контроллером.

 

и второй вопрос :( у меня почему-то пишется в базе пароль в открытом виде....

Link to comment
Share on other sites

встречался с такой проблемой.

вот мой код:

 

<?php
error_reporting(0);
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
$errors = array();
  if (!Validate::isLoadedObject($cart))
   $errors[] = 'Корзина не сформирована';
  if ($cart->getOrderTotal() <= 0)
   $errors[] = 'Добавьте в корзину 1 или более товаров';
  if (!Tools::getValue('email') || !Validate::isEmail(Tools::getValue('email')))
   $errors[] = 'Эл.адрес не заполнен или содержит ошибку';
  if (!Tools::getValue('phone_mobile') || !Validate::isPhoneNumber(Tools::getValue('phone_mobile')))
   $errors[] = 'Телефон не заполнен или содержит ошибку';
  if (!Tools::getValue('address1'))
   $errors[] = 'Адрес не заполнен или содержит ошибку';

   if (sizeof($errors))
   {
 $return = array(
  'hasError' => !empty($errors),
  'errors' => $errors
 );
 die(Tools::jsonEncode($return));
   }
   else
   {

  $email = $_POST['email'];
 $addrLastName = ($_POST['lname']?$_POST['lname']:'Клиент ');
  $addrFirstName = ($_POST['fname']?$_POST['fname']:'FictName');
 $_POST['address1'] = ($_POST['address1']?$_POST['address1']:'Адрес не указан');
 $_POST['city'] = ($_POST['city']?$_POST['city']:'CityEmpty');
   $customer = new Customer();
   $customer->validateControler();
   $customer->passwd = md5(time()._COOKIE_KEY_);
   $customer->firstname = $addrFirstName;
   $customer->lastname = $addrLastName;
   $customer->email = $email;
   $customer->active = 1;
   $customer->is_guest = 0;
   $customer->add();
   $address = new Address();
   $address->validateControler();
   $address->id_country = intval(Tools::getValue('id_country'));
   $address->id_state = intval(Tools::getValue('id_state'));
   $address->firstname = $addrFirstName;
   $address->lastname = $addrLastName;
   $address->phone_mobile = Tools::getValue('phone_mobile');
   $address->other = Tools::getValue('other');
   $address->address1 = Tools::getValue('address1');
   $address->city = ' ';
   $address->alias = 'quickorder';
   $address->id_customer = $customer->id;
   $address->save();
   $cart->id_address_invoice = (int)($address->id);
   $cart->id_address_delivery = (int)($address->id);
   $cookie->id_customer = intval($customer->id);
   $cookie->customer_lastname = $customer->lastname;
   $cookie->customer_firstname = $customer->firstname;
   $cookie->passwd = $customer->passwd;
   $cookie->logged = 1;
  $cookie->email = $customer->email;
  die(true);
 }

Link to comment
Share on other sites

что-о не понял, а пароль будет какой? автоматом чтоль генерится?

 

поправил

 

$customer->is_guest = 0;

 

и мой код сразу заработал..

 

Большое спасибо.

пароль тоже шифруется.

Edited by vscorpion (see edit history)
Link to comment
Share on other sites

А зачем вам столько валидатов? Не проще ли сделать

<input required type="email"...
<input required type="tel"...
итд

Таким образом за один проход вы проверяете и сам факт заполнения поля, и его корректность.

Link to comment
Share on other sites

×
×
  • Create New...