Forums

Re-Register and updated information from user

avan 14 Jun, 2010
First off, let me say that ChronoForms and ChronoConnectivity are awesome. I have been able to create and add custom fields to new users that register, and make a whole new form to manage volunteer hours in a matter of days. I cant think of any other module that is so powerful and flexible. Thanks for this module.


So here comes the question I have used ChronoForms to add a bunch of fields to the normal joomla registration process, and it all works great. What I need to allow the users to do is modify their registration information.

Originally, I added a link in a menu that they can select that takes them to the form that I created for registration, but I get to the point where I get this message.

You can not re-register while you are already signed in ...

Is there anyway for users to modify the information that they originally registered (including additional fields and password) and have is synchronize with the normal joomla user without removing the original user and re-registering ?

Thanks in Advance
avan
GreyHead 14 Jun, 2010
Hi avan,

The quick answer is to make a copy of the form and *don't* enable the Joomla Registration plugin for the copy.

Bob
nml375 14 Jun, 2010
Hi avan,
This is fully possible, though you'll need a separate form (as you've noticed, the registration plugin will prevent the form from loading if you are logged in).

To update "extended" (not stored in #__users) information, this is fairly trivial. Start with creating a form with inputs matching each field of the database table. Next, you'll need to add some php-code to allow us to update an existing record rather than register a new one.

This can be done in two ways, though the code posted here is safer in case the user tries to update someone else's data:
on Submit - Before Email:
<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "SELECT %s FROM %s WHERE %s = %s";

$primary = 'cf_id'; //Primary key of the database table, Chronoform uses cf_id
$userid = 'cf_user_id'; //Table row name holding the user id, Chronoform uses cf_user_id
$dbname = '#__chronoforms_myuser'; //Table name

$db->setQuery(
  sprintf(
    $query,
    $db->nameQuote($primary),
    $db->nameQuote($dbname),
    $db->nameQuote($userid),
    $db->Quote($user->id)
  )
);

$uid = $db->loadResult();
JRequest::setVar($primary, $uid, 'post');
?>


All you then have to do, is to setup the DB Connection in the same manner as you did with your registration form.

To update the User record, we'll need some further code:
<?php
$user =& JFactory::getUser();

$post = JRequest::get( 'post' );
unset($post['id']);
unset($post['usertype']);
unset($post['gid']);

$user->bind($post);
$user->save(true);
?>

This code is written from memory and untested, remember to make sure $primary, $userid, and $dbname is set accordingly. To alter the data in #__users, your form fields must have matching names; 'username' for the loginname, 'email' for the email address, 'password' and 'password2' for updating the password. The code will not allow one user to change their user id, usertype or group membership.

/Fredrik
avan 14 Jun, 2010
Thank you so much for both of your quick replies. (Just another reason why ChronoEngine Rocks 😀 )

I will try this out and let you know how it goes.
olaeblue 22 Nov, 2011
Hi, Trying to follow description from nml375 but struggling. I just want a simple form to change the password of a logged in user.

Have created 5 fields name, username, email, password and password_confirm.

I will prepopulate name, username, and email using php, and lock them so user cant change them (might make them hidden fields) then ask for a new password in the password boxes, but how do I use the code snippets?
Are they custom code actions on submit? If so what about DB save don't I need to do that to associate the database?

I'm probably being stupid but any help gratefully accepted.😶
nml375 22 Nov, 2011
Hi olaeblue,
If you are trying to change the user's password (as stored in the Joomla user database); you need the second piece of code - also, you should not store the plaintext password in a separate database table.

Updating the user's password requires two items in the associative array; being 'password' and 'password2'; which must match eachother. If you'd like to restrict the possible changes to password only, I suggest this modified code:
<?php
$user =& JFactory::getUser();

$opts = array();
$opts['password'] = JRequest::getString('password');
$opts['password2'] = JRequest::getString('password_confirm');

$user->bind($opts);
$user->save(true);
?>


This code was written for CF3.x, I believe the equivalent would be an custom code action in CF4.
There is an error in the previous post, as it lacks the call to the save() method of the user object. I'll update that shortly...

/Fredrik
olaeblue 23 Nov, 2011
Thanks. That worked perfectly
This topic is locked and no more replies can be posted.