Forums

How to autofill text area with joomla user's username?

innoads 06 Aug, 2014
Hello,

I have tried really hard to find out how to autofill text area with joomla user's username. But although reading lots of posts and the documentation I could not get it to work.

Let's say I have a form with just one text field "username" and a submit button. What do I have to do on setup page when using Wizard designer to make it autofill with the logged in user's username? I understand that it has to do with custom code and maybe also DB read. And I need to fill in some custom code, but I don't now what and where exactly.

Could anyone with experience give me a short step by step info how to set it up? That would be great 😀

Thanks and regards,
Andy
marcinwolejko 06 Aug, 2014
Answer
1 Likes
Hi.
I managed to get this to work.
Try inserting a new field in a custom code or header an paste the following code:
<?php
$user =& JFactory::getUser();
$name = '';
$email = '';
$readonly = '';
if ( $user->id > 0 ) {
  $name = $user->name;
  $email = $user->email;
  $readonly = "readonly='readonly'";
}
?>
<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="name" size="250">Insert your label for text field here</label>
  <input id="name" value="<?php echo $name; ?>" name="name" 
    type="text" <?php echo $readonly; ?> />
</div>


That should do the job🙂
marcinwolejko 06 Aug, 2014
Btw, it worked in CFv4 and I'm not quite sure it works in v5. But it's worth trying😉
innoads 06 Aug, 2014
@marcinwolejko: Thank you, this works well in V5, too!

@GreyHead: Thank you, I have read that FAQ already 5 times befor I asked the question, but it was not sure to me how and where to output the data in V5.

For everyone interested, it works for id, name, username, and email when you just put following code into a custom code FIELD in the Designer (nothing neccessary in Setup):
<?php
$user =& JFactory::getUser();
$form->data['id'] = $user->id;
$form->data['email'] = $user->email;
$form->data['name'] = $user->name;
$form->data['username'] = $user->username;
?>

<div class="ccms_form_element label_over" id="email_container_div" style="">
  <label for="id" size="250">ID</label>
  <input id="id" value="<?php echo $id; ?>" name="id" 
    type="text" <?php echo $readonly; ?> />
</div>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="name" size="250">Name</label>
  <input id="name" value="<?php echo $name; ?>" name="name" 
    type="text" <?php echo $readonly; ?> />
</div>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="username" size="250">Username</label>
  <input id="username" value="<?php echo $username; ?>" name="username" 
    type="text" <?php echo $readonly; ?> />
</div>

<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="email" size="250">Email</label>
  <input id="email" value="<?php echo $email; ?>" name="email" 
    type="text" <?php echo $readonly; ?> />
</div>


Maybe it is not the most elegant solution, but it works for me.
innoads 06 Aug, 2014
.. well, I just realized, that the email part is not working! It outputs an error in the text field saying, that email is not displayed because of spam protection (I have Honeypot enabled). And submitting the form results in an endless loading..

Maybe someone knows how to get that to work, too?
innoads 06 Aug, 2014
I think think the code I posted is not working at all if you want to send an email including that data. Sorry, I was too rash 😶
innoads 06 Aug, 2014
The same for marcinwolejko's code. It is displaying the user data, but it is not included in mail when send on submit.
marcinwolejko 07 Aug, 2014
Hi innoads.
I've put the code in a custom element PHP/ HTML (CFv4), gave the element name: username, id: username.
Then in the email template action just put the username in brackets {username} and on test email I've got it to work and send all the data I needed.
innoads 07 Aug, 2014
Hi marcinwolejko,

I have set it up pretty similar in V5. Used your code. Only difference is that for custom element it is only possible to set the label in the designer (not name or id). It was however inserted automatically into the mail template like <tr><td>Custom</td><td>{custom}</td></tr> in Email element on setup page.

But does not send the value via mail, only the label Custom.
Max_admin 07 Aug, 2014
1 Likes
in v5 the custom element doesn't get a name or id, because it may not contain an input at all, or may contain many, so its the user's duty to include the input's name in the email if they do so!🙂

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
innoads 07 Aug, 2014
That makes sense and works fine with the username.

What I still do not get to work is joomla user's email. If I use email instead of name in marcinwolejko's code following is displayed in the form field as plain text (and email value is empty):

<script type='text/javascript'> <!-- var prefix = 'ma' + 'il' + 'to'; var path = 'hr' + 'ef' + '='; var addy92611 = 'MY-EMAIL' + '@'; addy92611 = addy92611 + 'gmail' + '.' + 'com'; document.write('<a ' + path + '\'' + prefix + ':' + addy92611 + '\'>'+ addy92611+ '<\/a>'); //-->\n </script><script type='text/javascript'> <!-- document.write('<span style=\'display: none;\'>'+ 'Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!'+ '</'+ 'span>'); //--> </script>

PS: The German output means "This e-mail adress is protected against spambots! To display it JavaScript must be enabled" (which is enabled in browser)
innoads 07 Aug, 2014
.. while no spam protection element is used.
This topic is locked and no more replies can be posted.