How to use Joomla User Information in Chronoforms
Lets pretend you need some user information from a logged in user in your form. But you don't want to have the user type in his user info again and again, because he already did it while registering for the Account.
So you are looking for a possibility to automatically fill in the users name, username, e-mail adress into the chronoform, so the user doesn't have to type it again.
Here's the (easy!) solution with a little bit help of the Joomla Framework:
Enter (or copy & paste) the following code at the beginning of your chronoform HTML-Code:
In the form itself use the following syntax to fill either inputboxes or hidden fields with the wanted user information like that:
Fill the user info into an inputbox:
The "value"-tag and the following snippet of php will insert the username ($user->name) into the text field so the user is abled to leave it as it is or change it if needed. Convenient.
Fill the user info into a hidden field:
This will add a hidden field to your form which will transfer the username to the recipient without printing it on the screen. Usefull if you want to transfer lot of info in the form, which the user doesn't need to see, but the recipient does.
If you want to user other info than the user name you just replace the php snippet part with:
For the username, the users e-mail address and so on...
A full list of the possible user info you can use with this function is available here:
http://api.joomla.org/Joomla-Framework/User/JUser.html
Just a small feature, but comes in handy from time to time. :-)
Haphe phun, Savant
Lets pretend you need some user information from a logged in user in your form. But you don't want to have the user type in his user info again and again, because he already did it while registering for the Account.
So you are looking for a possibility to automatically fill in the users name, username, e-mail adress into the chronoform, so the user doesn't have to type it again.
Here's the (easy!) solution with a little bit help of the Joomla Framework:
Enter (or copy & paste) the following code at the beginning of your chronoform HTML-Code:
<?php
// Get user-information from Joomla
$user = &JFactory::getUser();
?>
In the form itself use the following syntax to fill either inputboxes or hidden fields with the wanted user information like that:
Fill the user info into an inputbox:
<input type="text" name="username" id="username" size="20"
maxlength="40" value="<?= $user->name; ?>" />
The "value"-tag and the following snippet of php will insert the username ($user->name) into the text field so the user is abled to leave it as it is or change it if needed. Convenient.
Fill the user info into a hidden field:
<input type="hidden" name="username" id="username"
value="<?= $user->name; ?>" />
This will add a hidden field to your form which will transfer the username to the recipient without printing it on the screen. Usefull if you want to transfer lot of info in the form, which the user doesn't need to see, but the recipient does.
If you want to user other info than the user name you just replace the php snippet part with:
<?= $user->username; ?>
<?= $user->email; ?>
<?= $user->password_clear; ?>
<?= $user->id; ?>
...
For the username, the users e-mail address and so on...
A full list of the possible user info you can use with this function is available here:
http://api.joomla.org/Joomla-Framework/User/JUser.html
Just a small feature, but comes in handy from time to time. :-)
Haphe phun, Savant
Hi Savant,
Thanks - good info!
Bob
PS Line breaks seem to have gone to pot at the moment so anyone using this code cut and paste with care!!!
Thanks - good info!
Bob
PS Line breaks seem to have gone to pot at the moment so anyone using this code cut and paste with care!!!
Great! Is there a way to include user info from custom fields made with Community Builder?
Hi Astrid,
Yes, I think that the Profile PlugIn is the simplest way of doing this. There have been a couple of recent conversations about it here.
Bob
Yes, I think that the Profile PlugIn is the simplest way of doing this. There have been a couple of recent conversations about it here.
Bob
Hi I have been puzzling over getting logged in users name to appear in a textbox and save this along with other inputed information for the form. But how do you get the name to be saved in the chronoform database.
I can get the logged in name into a box but don't now how to configer the form to save it
Regards
Irvin
I can get the logged in name into a box but don't now how to configer the form to save it
Regards
Irvin
Hi irvinmetcalf,
You can put code into the default value of a form input but I'd probably use a Custom Code box and put the code in there:
This code checks first to see if there is a value set in the form data (for example if the form is being relaoded after an error) and uses that in preference to the saved version.
Bob
You can put code into the default value of a form input but I'd probably use a Custom Code box and put the code in there:
<?php
if ( isset($form->data['username']) && $form->data['username'] ) {
$username = $form->data['username'];
} else {
$user =& JFactory::getUser();
$username = $user->username;
}
?>
<input type='text' name='username' id='username' value='<?php echo $username; ?>' />
This code checks first to see if there is a value set in the form data (for example if the form is being relaoded after an error) and uses that in preference to the saved version.
Bob
Is this not insecure allowing a user to change their username, to "Admin" for example. This needs to be completed after the fact.
In other words, we need to acquire Username / UserID after form submission on server side scripting. Any ideas how this can be done?
In other words, we need to acquire Username / UserID after form submission on server side scripting. Any ideas how this can be done?
Hi YesAdam,
True, and I wouldn't use the value like that. You should at the very least use serverside validation to check the result.
Bob
True, and I wouldn't use the value like that. You should at the very least use serverside validation to check the result.
Bob
Thank you for the information. It helps me to show data from joomla user.
But how to save other data to database?
This is what I have done.
Create a form using Form Wizard. I click event tab and drag custom code inside On Load. I put your code in it with form html code.
I display name, and I ask user to fill other information (registration for an event's information).
After that, how should I save those to a database?
Thank you,
Selly
But how to save other data to database?
This is what I have done.
Create a form using Form Wizard. I click event tab and drag custom code inside On Load. I put your code in it with form html code.
I display name, and I ask user to fill other information (registration for an event's information).
After that, how should I save those to a database?
Thank you,
Selly
Hi Selly,
Use a DB Save action to save data to the database. The form input names must match the names of the columns in the database table.
Bob
Use a DB Save action to save data to the database. The form input names must match the names of the columns in the database table.
Bob
Hi Bob, Thank you for your answer.
Yes, I already used DB Save action and all input names already matched with database table's columns, but after I click submit, no data is being saved to a database.
Is there a step that I missed? What should I add in submit button? Or in form tag, should I use onsubmit=something?
Thank you.
Yes, I already used DB Save action and all input names already matched with database table's columns, but after I click submit, no data is being saved to a database.
Is there a step that I missed? What should I add in submit button? Or in form tag, should I use onsubmit=something?
Thank you.
Hi s_meliana,
There's nothing else that you have missed. But something isn't right.
Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here.
Note: if you are using the Easy Wizard you may need to switch to the Advanced Wizard to do this; if you want to continue to use the Easy Wizard please make a copy of your form first and add the Debugger action to the copy.
Bob
There's nothing else that you have missed. But something isn't right.
Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here.
Note: if you are using the Easy Wizard you may need to switch to the Advanced Wizard to do this; if you want to continue to use the Easy Wizard please make a copy of your form first and add the Debugger action to the copy.
Bob
Hi - I'm new to chronoforms and have version 4.0.1 installed with joomla 2.5.17.
I've read through this post and a few others but still can't seem to get a users information displayed on the form or sent via email. I've even installed the action load_user_info_gh from GreyHead. Can anyone help walk me through the setup?
Heres what I have so far.
Using the form wizard I created a simple form for a member to request cancellation of their paid account (see attached). 1 check box and a submit button which sends an email to a static address.
[attachment=0]form preview.png[/attachment]
in the Custom Element I have the following code ( Pure code unchecked)
<input type="hidden" name="name" id="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" id="email" value="<?php echo $email; ?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
using input type="text" comes up blank to.
Using the method described in this post I have the following events
On Load
Show html
Custom Code
Style Form
Load User Info [GH] - with or without it doesn't work
On Submit
Load User Info [GH] - with or without it doesn't work
DB Save
Email - using Auto template generation
Redirect User
[attachment=1]events.png[/attachment]
I've read through this post and a few others but still can't seem to get a users information displayed on the form or sent via email. I've even installed the action load_user_info_gh from GreyHead. Can anyone help walk me through the setup?
Heres what I have so far.
Using the form wizard I created a simple form for a member to request cancellation of their paid account (see attached). 1 check box and a submit button which sends an email to a static address.
[attachment=0]form preview.png[/attachment]
in the Custom Element I have the following code ( Pure code unchecked)
<input type="hidden" name="name" id="name" value="<?php echo $name; ?>" />
<input type="hidden" name="email" id="email" value="<?php echo $email; ?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
using input type="text" comes up blank to.
Using the method described in this post I have the following events
On Load
Show html
Custom Code
<?php
// Get user-information from Joomla
$user = &JFactory::getUser();
?>
Style Form
Load User Info [GH] - with or without it doesn't work
On Submit
Load User Info [GH] - with or without it doesn't work
DB Save
Email - using Auto template generation
Redirect User
[attachment=1]events.png[/attachment]
Hi dksf,
There is a FAQ on this that you should read. The main problem is that you have used:
Bob
There is a FAQ on this that you should read. The main problem is that you have used:
<input type='text' name='username' id='username' value='<?php echo $username; ?>' />
while it needs to be <input type='text' name='username' id='username' value='<?php echo $user->username; ?>' />
Bob
This topic is locked and no more replies can be posted.