Forums

User registration by email with autopassword and autologin

oloccina 19 May, 2016
Hello,
I guess this is not possible to do with chronoforms, but asking doesn't hurt🙂

I need a simple form with only email input field, this should create a new user
using as username the first part of the email (before @), generate automatically a random password, and autologin the user after submission.

An email should then be sent to the user with his account details.

I know that there a couple of extensions already able to do this, but I was looking for a more flexible solution using chronoforms.

Thank you,
Nic
oloccina 19 May, 2016
I guess the only thing that is not possible with the registration action:
using as name and username the first part of the email (before @)
GreyHead 19 May, 2016
Hi oloccina,

You can do that if you need to. Add a Custom Code action before the Joomla! Registration and use PHP to remove the email domain and add the result to a new form variable. Something like this:
<?php
$user_name = explode('@', $form->data['email']);
$form->data['user_name'] = $user_name[0];
?>

Bob
oloccina 19 May, 2016
Thanks!
but what do I insert int he "username" field in the "joomla registration" action?

I just inserted the php code you suggested ina custom code before the action,
and filled in only the "email" field in the action,

th is my form code right now

<div class="form-group gcore-form-row" id="form-row-email"><label for="email" class="control-label gcore-label-left">E-mail</label>
<div class="gcore-input gcore-display-table" id="fin-email"><input name="email" id="email" value="" placeholder="la tua email" class="validate['required','email'] form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" type="text" /></div></div><div class="form-group gcore-form-row" id="form-row-button2"><div class="gcore-input gcore-display-table" id="fin-button2"><input name="button2" id="button2" type="submit" value="Accedi al corso!" class="btn btn-default form-control A" style="" data-load-state="" /></div></div>


this is my setup attached
oloccina 19 May, 2016
Ok,
got it working,

it register correctl a new user but it still uses
the full email address for both name and username,
it doesn't only use th epart before @

I attach this form for your reference

what I didi to get it working was just fill in the name and username password
using the "email" value

sorry, I am not very tech savvy :-(
GreyHead 20 May, 2016
Hi oloccina,

In the Registration action you need to put user_name in the Username and Name boxes.

Bob
oloccina 20 May, 2016
great, that didi the trick!

another issue that cime to my mind is that many people register using info@...
as their email address, thus the generated usernames would be all the same,
could you tell me where can I find another code snippet that can check if the username
is already existing and in case add numbers after it, like info1, info2, info3 ?

Thank you!
GreyHead 20 May, 2016
Hi oloccina,

Hmmm . . . there are a couple of ways to do this, I think I'd try something like this:
<?php
$user_name = explode('@', $form->data['email']);
$user_name = $user_name[0];
$db = \JFactory::getDBO();
$query = "
    SELECT `username`
        FROM `#__users`
        WHERE `username` LIKE '{$user_name}%' ;
";
$db->setQuery($query);
$usernames = $db->loadColumn();
$temp = $user_name;
if ( count($usernames) > 0 ) {
  while ( in_array( $temp, $usernames ) ) {
    $temp = $user_name.'_'.rand(1111, 9999);
  }
}
$form->data['user_name'] = $temp;
?>
!! not tested and may need debugging !!

Bob
oloccina 20 May, 2016
Thanks Bob,
I'll get more help on stackoverflow
for debugging the code,
but I really appreciated you taking the time to post it.

I am now managing all of my many forms with Chronoforms
and I am always impressed at how flexible it is :-)
oloccina 23 May, 2016
Tested!
this is the error I receive when filling the form with
an email like info@mydomain.com

1064
YOU HAVE AN ERROR IN YOUR SQL SYNTAX; CHECK THE MANUAL THAT CORRESPONDS TO YOUR MYSQL SERVER VERSION FOR THE RIGHT SYNTAX TO USE NEAR ''INFO%''' AT LINE 3 SQL=SELECT `USERNAME` FROM `#__USERS` WHERE `USERNAME` LIKE 'INFO%'' ;
GreyHead 23 May, 2016
Hi oloccina,

It did need debugging - there was an extra quote ' after the % - there should be one, not two.

Bob
oloccina 23 May, 2016
working like a breeze now, thank you!
oloccina 23 May, 2016
coffee bought! :-)
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount

{item:title} {images:#}