Forums

How to pre-fill user name into form field?

HerKle 28 Nov, 2014
Hi,

in a form, available only for registered users, so that the user data are already known, I want to to show the respective fields already filled with user-id and user-email - but still writable.

So, I used in "Setup" section a custom code:
$user =& JFactory::getUser();
if ( $user->id > 0 ) {
$form->data['user_name'] = $user->name;
$form->data['user_email'] = $user->email;
}

In the "Designer" section I tried to make these data visible by "Edit element settings" -> tab "General" - > field "Field value".
But no manner of writing the variable makes the data visible when loading the form.

In this article I found a possible solution, but this seems to require a custom code field in the "Edit element settings":
http://www.chronoengine.com/faqs/3119-how-can-i-ask-new-users-for-information-and-not-ask-logged-in-users

Where should I put that html code, if this would be the only solution?
But I think there must be some easier and more elgant way to prefill those two fields?!

Regards,
Herbert
BNDragon 28 Nov, 2014
Hi Herbert,

It's quite easy, create your form, assuming your two fields "user_name" and "user_email" and a submit button, then in Events, before the HTML action set a custom action and insert your code. Please, see that your code is not html is a php code, so it need tags
<?php $user =& JFactory::getUser();

if ( $user->id > 0 ) {

$form->data['user_name'] = $user->name;

$form->data['user_email'] = $user->email;

}?>

And this should work,
BN
HerKle 28 Nov, 2014
Thanks BNDragon,

but you describe exactly what I did – including php tags of course – but the fields remain empty.
So that's why I thought that I have to pass over this information anywhere else.

Regards,
Herbert
BNDragon 28 Nov, 2014
Hi Herbert,

Can you post some prints?

Or PM the link to backend form link and give me an access token to take a look?

BN
HerKle 28 Nov, 2014
Sorry, but I have it still on localhost workaround – unpublished yet in www.
I come back later when I have uploaded the site. Thanks anyway!
Herbert
BNDragon 28 Nov, 2014
Hi Herbert,

Just post some prints, it could help me understand your problem.

BN
GreyHead 28 Nov, 2014
Hi Herbert,

To make them show up and be editable you need to add form Text Box elements in the Designer tab with the names user_name and user_email as well as the Custom Code.

Bob
HerKle 29 Nov, 2014
Hi Bob,
I did all that, as I wrote in my original post - but what do you mean with "custom code" in designer section?

I got the user-data with custom code element already in On Load event. I don't understand you.

Regards,
Frank
GreyHead 30 Nov, 2014
Hi Frank,

Sorry, I misunderstood that part of your post.

What names have you given the Text inputs - you don't need to do anything with their values.

Bob
This topic is locked and no more replies can be posted.