Get content article information with an RSForm!Pro form

Using the Content Plugin you can display an RSForm!Pro form within a Joomla! Content article. Due to RSForm!Pro's extremely flexible nature, using a little bit of custom scripting you can pull article-related information from the database. This can be very useful if you have the same form published within multiple articles.


In this article we will provide a step-by-step guide on getting information such as the content article's title, the author's name and email address, based on the article's ID found in the URL.


The Content Plugin


To display an RSForm!Pro form in a Joomla! content article, you will need to download and install the Content Plugin. After doing so, simply add the following syntax:


{rsform x} - x being the form's ID (you can get it in the backend Manage Forms area)


anywhere in the Joomla! Content article's text. The Content Plugin will recognize it and replace it with the actual form's layout.


The form and custom code examples


We will not describe the form's field structure as this really depends on your particular scenario. However, these examples require that you have a hidden field which will store the article information retrieved by our code.


The code in the following examples will need to be added in the Components >> RSForm!Pro >> Manage Forms >> edit your form >> Properties >> PHP Scripts tab >> Script called on form process area:


Getting the author's name

$articleid=JFactory::getApplication()->input->getInt('id');  //gets the article's ID from the URL
$db = JFactory::getDBO();  //opens a new database connection
$db->setQuery("SELECT `created_by` FROM #__content WHERE `id`='$articleid'");
//gets the author's ID
$result=$db->loadResult();
if($result){
$owner = JFactory::getUser($result);  //gets the author's name based on his previously retrieved ID
$_POST['form']['hidden']=$owner->name;  //assigns the author's name as the hidden field's value
}

Getting the author's email address


$articleid=JFactory::getApplication()->input->getInt('id');  //gets the article's ID from the URL
$db = JFactory::getDBO();  //opens a new database connection
$db->setQuery("SELECT `created_by` FROM #__content WHERE `id`='$articleid'");
//gets the author's ID
$result=$db->loadResult();
if($result){
$owner = JFactory::getUser($result);  //gets the author's name based on his ID
$_POST['form']['hidden']=$owner->email;  //assigns the author's email address as the hidden field's value
}

Getting the article's title


$articleid=JFactory::getApplication()->input->getInt('id');  //gets the article's ID from the URL
$db = JFactory::getDBO();  //opens a new database connection
$db->setQuery("SELECT `title` FROM #__content WHERE `id`='$articleid'");
//gets the article's title based on its ID
$result=$db->loadResult();
if($result){
$_POST['form']['hidden']=$result;  //assigns the article's title as the hidden field's value
}

You will then be able to use the hidden field's placeholder - {name_of_field:value} - anywhere in the User / Admin emails configuration and text, as well as in the Thank you message and in the View Submissions and Submissions Directory menu items.


19 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that