Forums

Usernames as hidden fields

irti 10 Feb, 2010
Hi ... this may seem a very simple question but am a complete newbie at this. I have some forms on my site which a user can fill out only if he/she is logged in. I want to capture the username using hidden fields. How do i do that?? ( i am using joomla and cb)
GreyHead 10 Feb, 2010
Hi irti,

The username of a logged in user is available from the Joomal User object.
<?php
$user =& JFactory::getUser();
echo $user->username;
?>

You probably don't want to echo it but you can if you need to.

If you want to record the user, then the user id is sometimes easier to work with than the username.

Bob
irti 10 Feb, 2010
Hi Bob
Thanks for the immediate reply... As i said i am a complete newbie at this....so i dont have the slightest idea where to place that code. I am sorry but could you please elaborate a little. Thanks again for helping me out with this!!!!
GreyHead 10 Feb, 2010
Hi irti,

OK, but to do that I need more information from you. What exactly do you mean when you say you 'need to capture the username'??

Bob
irti 11 Feb, 2010
Hi Bob
I am trying to build up forms which require a user to log in first. So the user logs into the site. (Actually its an intranet), and then he can visit the forms page to fill up forms. Over here i do not want to give a name field for the user to fill in( As I connect my joomla intranet to excel via odbc connenctor for analysis purposes, and if they fill in their own names it messes around with the analysis results as the same person can post his name in a lot of different ways eg. Mr. XYZ or XYZ or X Z. I hope you can understand me) So i want to add a hidden field in this form which would just capture the username of this person(as he has already logged in to the intranet) and post it in a field in the table like the other entries. I searched on the intranet and found out that there was a way to do it but am a total noob at programming. I just know the very basics. So if you could please help me out with this I would be grateful. Thanks a lot
GreyHead 11 Feb, 2010
Hi irti,

Try this code at the beginning of the Form HTML
<?php
if ( !$mainframe->isSite() ) { return; }
$user =& JFactory::getUser();
if ( !$user->id ) {
  $mainframe->redirect('index.php');
}
?>
<input type='hidden' name='username' value='<?php echo $user->username; ?>' />

This will redirect any unsigned in users to the site home page and will put the username of anyone else into the username hidden input.

Bob
irti 13 Feb, 2010
thanks ....works perfectly.....amazing ...thanks for the help once again
This topic is locked and no more replies can be posted.