Forums

Correct code to display and use Joomla user's name and id

phicts 07 Nov, 2015
How to correctly get the user_id of the logged in Joomla user and display it in chronoform for saving into database? I am noob to this. Please excuse me. Below is the code I use and I knew there are errors in it because the user id won't appear in the form:

<?php
$user =& JFactory::getUser();
$form->data['email'] = $user->email;
$form->data['name'] = $user->name;
$form->data['user_id'] = $user->id;
?>

<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="email" size="250">Email</label>
  <input id="email" value="<?php echo $email; ?>" name="email" 
    type="text" <?php echo $readonly; ?> />
</div>
<div class="ccms_form_element label_over" id="name_container_div" style="">
  <label for="user_id" size="100">User Id</label>
  <input id="user_id" value="<?php echo $id; ?>" name="id" 
    type="integer" <?php echo $readonly; ?> />
</div>


Attached is the result of the form.
phicts 07 Nov, 2015
I need to insert into the database table the userid of the registered user submitting the form. But I am correct in thinking that when I create the db table automatically through the built-in function of chronoform, that useid field will be automatically created?

And please point to me the errors in the code and the corrections. Thank you,
GreyHead 07 Nov, 2015
Answer
Hi phicts,

If there is a column in the table named user_id then ChronoForms will save the Current User ID into that column.

Is that the complete code? I'm surprised that anything appears?
For example, you have
<input id="name" value="<?php echo $name; ?>" name="name" 
    type="text" <?php echo $readonly; ?> />
but neither $name nor $readonly are defined in the code you posted.

I would do this by adding a Custom Code action in the On Load event of your form before the HTML (Render Form) action and putting the first part of your code there.
<?php
$user = \JFactory::getUser();
$form->data['email'] = $user->email;
$form->data['name'] = $user->name;
$form->data['user_id'] = $user->id;
?>
Then in the Designer tab add the three matching text inputs with the names 'email', 'name' and 'user_id' and in each of them add readonly=readonly in the Extra Params box

Bob
phicts 11 Jan, 2016
Bob, thanks a lot for the exact code and the very clear instructions that enabled me to show everything correctly. I put all the codes in a custom field though. I will try your recommendation to separate the codes which I think is better. After all, you're the master.🙂
Thanks again.
GreyHead 11 Jan, 2016
Hi phicts,

Where you put the code is entirely a question of personal preference - either will work. I prefer to put any code for getting or working with the form data in the On Load event and keep the Custom Code elements in the Design tab for managing the display of the data.

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