Display PHP variables by default when form is shown
With RSForm!Pro you can display variables like user information (if the user is logged in) directly in the form components that you create.
Example 1.
Assuming that you want to create an e-mail form component, and by default, if the user is logged in, to enter it's registration e-mail in the Email field. Here's how to do it:
- Create a textbox component
- In the Default Value area, type this code:
Example 2.
This piece of code (including the //<code> tags) loads the user information in the $user variable. Here's what you can use:
- $user->get('id') = user id
- $user->get('username') = the username
- $user->get('name') = registration full name
- $user->get('email') = registration email
Example 3.
Displaying data from the user's profile information is possible as well. Example for the region:
//<code> $user = Joomla\CMS\Factory::getUser(); $profile = Joomla\CMS\User\UserHelper::getProfile($user->id); return $profile->profile['region']; //</code>
- The $profile variable loads the logged in user profile data
- Tip: other profile information can be found by performing a print_r on the $profile variable
- Important: the "User - Profile" plugin has to be enabled in order for this to work
Example 4.
You can use a code similar to the one below to pull information from the database and add it as a default value of a field:
//<code> $db = Joomla\CMS\Factory::getDbo(); $db->setQuery("SELECT `column_name` FROM `#__table_name` WHERE `column_name`='value' LIMIT 1"); return $db->loadResult(); //</code>
Note: Please remember to replace column_name and table_name with the actual names of your column and table.
148 persons found this article helpful.
You Should Also Read
Custom PHP code HOT |
Custom validation rules HOT |
PHP Scripts HOT |
Controlling the calendar HOT |
How can I prevent the user from selecting a date in the calendar field HOT |