name; }?>I then placed it like this in the field code:Your nameguest) { echo ""; } else { echo $user->name; }?>" maxlength="150" size="30" class="" title="" type="text" value="" name="input_text_1" />It does work well, when I open the form, the user's name is populated.But, the form's purpose is to add entries in my sql database through a "DB save" action. Then, I can display it as a spreadsheet within an article with the "Table JX" plugin.After having submitted the form, what I find in my database is the user ID ("172" for instance) in the "ch_user_id" field.What can I do so that "172" is replaced by the full name of my user ?Thanks😉"> Retrieving the user name in a form - Forums

Forums

Retrieving the user name in a form

fredfitaire 07 Nov, 2013
Hello,

I have chronoforms v4 installed in my site and have created a form, including a "label text" which purpose is to be filled with the name of the user. This form is intended to registered users only. The idea was to retrieve the name of the user and fill it automatically in this field.
I found somewhere (here in the forum ?) the following code:

<?php
$user =& JFactory::getUser();

  if ($user->guest) {
    echo "";
  } else {
     echo $user->name;
    }
?>


I then placed it like this in the field code:

<label>Your name</label><input value="<?php
$user =& JFactory::getUser();

  if ($user->guest) {
    echo "";
  } else {
     echo $user->name;
    }
?>" maxlength="150" size="30" class="" title="" type="text" value="" name="input_text_1" />


It does work well, when I open the form, the user's name is populated.
But, the form's purpose is to add entries in my sql database through a "DB save" action. Then, I can display it as a spreadsheet within an article with the "Table JX" plugin.
After having submitted the form, what I find in my database is the user ID ("172" for instance) in the "ch_user_id" field.
What can I do so that "172" is replaced by the full name of my user ?

Thanks😉
GreyHead 07 Nov, 2013
Hi fredfitaire,

A neater way to do this is to Use a custom code action in the On Load event with code like this:
<?php
$user =& JFactory::getUser();
$form->data['input_text_1'] = $user->name;
?>
Then ChronoForms will automatically set the value of input_text_1 for you.

ChronoForms automatically sets the value of the cf_user_id column to the current user id. You should not try to change this.

If you need to save the user name then add an input_text_1 column to the database table (though I would use a more meaningful input name).

Not that this is probably unnecessary as if you have the user id (from the cf_user_id column) you can always look up the user name:
<?php
$some_user =& JFactory::getUser('some_id');
echo $user->username;
?>


Bob
fredfitaire 07 Nov, 2013
Thanks a lot Bob, exactly what I needed !
This topic is locked and no more replies can be posted.