Extend RSForm!Pro with contacts

Download the sample form

In this example we will create a simple contact form that will retrieve the contact's email address from the default Joomla! users table.

1. First we will create the form(you can use the form creation wizard and choose the Simple contact form layout). In the wizard please make sure to disable the admin email, we will generate this email through our script. After the form is created you will only need to add an extra field(in this example we have used a dropdown) that will list all your available contacts(stored in the #__contact_details table). The script will be similar to the one presented here.

//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";

// Run the SQL query and store it in $results
// we will retrieve the user id, position and name for our dropdown
$db->setQuery("SELECT user_id, con_position, name FROM #__contact_details");
$results = $db->loadObjectList();

// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. 1|John - director
foreach ($results as $result) {
  $value = $result->user_id;
  $label = $result->name.' - '.$result->con_position;
  $items[] = $value.'|'.$label;
}

// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);

// Now we need to return the value to the field
// the rtrim() function is used so that we do not have an empty item generated by the new line of the last item
return $items;
//</code>

2. After the dropdown is generated we will only need to use a simple script in the PHP Email Scripts tab that will retrieve the correct email address from the default Joomla! users table based on the user id. This is done by adding the following lines in the Script called before the Admin Email is sent. field

//configure the FROM email that will be used in the Admin emails tab
$adminEmail['from']  = 'your_site_email@domain.com';

//retrieve the id of the contact selected by the user in the form
$selectedContact   = $_POST['form']['department'][0];

//retrieve the email of the selected id using the default Joomla! getUser() function 
//configure the TO email that will be used in the Admin emails tab
$adminEmail['to']  = JFactory::getUser($selectedContact)->get('email');

5 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that