Forums

Full demo showing getuser() to populate email and form

cbates 04 Nov, 2016
Hi All!
I apologize for the post, but I have been unable to make the getuser() work and have looked for a full demo, not just bits of code to guide me.

Currently, I am trying to add the logged in user id and name to a table and use it in an email once I submit a form. I have read the FAQ's and searched getuser in the forums and have not been able to get the code to work. I have attached screen shots to show what I have done so far.

I have added to the onload section the custom code:
<?php
    $user = \JFactory::getUser();
    $form->data['id'] = $user->id;
    $form->data['name'] = $user->name;
?>

I have a text boxe's with the id's: 'id' and 'name'
When I run the code shown above, nothing populates, but if a put an id in, the text boxe's populate.
What am I missing?

Any input would help, I have everything else working on my form, but this last bit.
Thanks!
Colleen
GreyHead 04 Nov, 2016
Hi Colleen,

That all looks OK.

Silly question but are you logged in the front end when you test - the results you are seeing would be what a guest would see?

Bob

PS I suggest that you don't use id for the user id - there is a chance of it getting confused with article ids or other record ids. I usually use user_id for clarity.
cbates 04 Nov, 2016
Oh my gosh!!!!! Stupid me!!!! I was just testing it from the back-end. It works great when logged in to the front end.
I agree about the id, I was just throwing an id in quickly to view what was happening. Now that the world is good, :-) I will clean everything up.

Thanks for pointing out the obvious!
Colleen
GreyHead 04 Nov, 2016
Hi Colleen,

You may need to add a little more PHP - to check that the user is logged in and redirect if not.
<?php
 $user = \JFactory::getUser();
 $form->data['id'] = $user->id;
if ( $user->id === 0 ) {
  $app = \JFactory::getApplication();
  $app->enqueueMessage('Please login');
  $app->redirect('xxx'); // add link to login form
}
$form->data['name'] = $user->name;
?>

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

VPS & Email Hosting 20% discount

{item:title} {images:#}