How to Dynamically Duplicate a Form in RSForm Using PHP

Note 1: Validating fields on dynamically duplicated forms is too complex to explain in one post – and that’s why it’s not included here. If you want help on this, then please contact us.
Note 2: We are assuming that you do not wish to save the fields in the database – if this is not the case then (you’ve guessed it!) contact us. Again, this is very complicated task and that’s why we elected not to include it here.
Note 3: We are assuming that you only have one submit button on your page.
Note 4: We are assuming that you want to duplicate the whole form, and not just parts of it.
Note 5: “Auto Generate Layout?” must be unchecked (we will explain later why).
Note 6: This post is targeted for those with solid PHP experience and advanced Joomla knowledge. If you’re not a programmer then we suggest you contact some Joomla Experts to do the below.

In our last post on RSForm, we explained how to how to assign dynamic values to fields by using some specially formed PHP code (that code only works in RSForm Pro). In this post, we will disucss how to dynamically duplicate a form in RSForm.

So, why does anyone need to duplicate a form with RSForm?

Let’s say, for example, that your company offers training services for other companies. A company requiring your training services wants to enlist some of its employees for one of your trainings – and so you need a page that contains multiple instances of the same form (one for each trainee) depending on the number of the trainees. There are two ways to do this: 1) Create a custom component to do this (which can be a tedious, complicated, and error-prone task) or 2) leverage the power and flexibility of RSForm to do this. We will focus on how to do this using the latter way.

If you have been using RSForm for some time (and if you have some technical background), you probably know that the form itself is available in a variable called $formlayout on form display and can be manipulated with a PHP script.

Script called on form display

Figure 1: “Script called on form display” textarea in RSForm. This is where one can manipulate the form layout prior to its display.

What does that mean? That means that if we write something like this $formLayout .= $formLayout; in the Script called on form display textarea (you can reach this textarea by clicking on a form in the backend, and then clicking Properties on the top of the form, and finally clicking on PHP Scripts under Scripts on the bottom left) the whole form will be duplicated. Try it, it’ll work!

But, just duplicating the form is not enough, as we need to ensure that the following 3 things are done:

  1. The <form> and </form> tags are not duplicated. This can be done with a simple find and replace using the following code:

    //remove the </form>.
    $duplicateForm = str_replace("</form>", "", $formLayout);
    //now we have duplicate content of the form without the </form> tags.
    $duplicateForm = preg_replace("#<form(.*)>#i", "", $duplicateForm);

  2. The submit button is not duplicated. This is a bit tricky, because the submit button is a field like any other field (or a component, as per RSForm’s terminology). A smart (and fast) way for doing this is to encapsulate the entry for the submit button between something like <!–ignoresubmitbutton–> and then remove anything between them when duplicating the form.
  3. The duplicated fields have different names and ids. This can be done by appending xyz (or another suffix/prefix of your choice) to the name and the id of each and every field and then do an str_replace that replaces xyz with the current number of the form.

Here’s what the script that is called on display of the RSForm to duplicate the form should be:

	//getting the ID of the customer from the session
	$session =& JFactory::getSession();
	$customerId = $session->get('customerId');
	//getting the number of trainees (which is the same as the number of forms) from the database
	$db = JFactory::getDbo();
	$query = $db->getQuery(true);
	$query = "Select num_trainees from #__training WHERE customer_id='".$customerId."' LIMIT 0, 1";
	$numForms = $db->loadResult();

	//creating a copy of the form without <form> and </form>
	$duplicateForm = str_replace("</form>", "", $formLayout);
	$duplicateForm = preg_replace("#<form(.*)>#i", "", $duplicateForm);

	//now remove the submit button area
	$arrDuplicateForm = explode('<!--ignoresubmitbutton-->', $duplicateForm);
	$duplicateForm = "";
	for ($i = 0; $i < count($arrDuplicateForm); $i++){
		if ($i == 1)
		$duplicateForm .= $arrDuplicateForm[$i];

 	//ensuring that the names and the ids are not the same in duplicated forms
	$duplicateForms = "";
	for ($i = 0; $ i < $numForms - 1; $i++){
		$duplicateForms .= str_replace("xyz", "".$i+1, $duplicateForm);
 	//adding all the generated forms to the original form
	$submitPosition = strpos($formLayout, "<!--ignoresubmitbutton-->");
	$formLayout = substr_replace($formLayout , $duplicateForms, $submitPosition, strlen("<!--ignoresubmitbutton-->"));

Now that we have duplicated the form, all we need to do is to format the information received in the email. But how can this be done?

Well, it's not that hard as it may seem. The first thing that we need to do is to save the $_POST information in the session (or in a table) by adding the following code to the textarea titled Script called on form process:

	$session =& JFactory::getSession();
	$session->set('forminformation', $_POST);

The second (and final) thing that we need to do is to retrieve the session information in the Script called before the Admin Email is sent textarea and then manipulate the $adminEmail to fit our needs. Here's the code:

	$session =& JFactory::getSession();
	$arrFormInformation = $session->get('forminformation');
	//now we can manipulate the $adminEmail the way we want
	//after finishing our work with $adminEmail, we need to clear the session variable forminformation

As we stated in the beginning of this post, one needs to have some decent PHP and Joomla experience to do the above. If you're not a technical person and have some tricky requirements on your RSForm (such as dynamically duplicating forms based on some criteria) that you can't do by yourself, then we're happy to tell you that we can help you! Just contact us and see how fast, affordable, efficient, and knowledgeable we are. We are, after all, the Joomla Experts.

5 Responses to “How to Dynamically Duplicate a Form in RSForm Using PHP”
  1. Comment by markus — May 19, 2013 @ 3:11 am

    Hi itoctopus .. amazing trick!

    I’m a newbie in this, but i tried to do this:
    I have a general form…
    I just need to some simple code to show the user name logged into Joomla in the new form.


    User logged into Joomla:
    +–My new form—-+

    Hi {username} please put your information below:

    [summit] [clear]

    +–end form—+

    I can put this in any free text…
    Do you have any example to help…

  2. Comment by markus — May 19, 2013 @ 3:33 am

    I’m using this simple code:

    $user = JFactory::getUser();
    echo $user;

    or can I use this, in your example:

    $session =& JFactory::getSession();
    $username = $session->get(‘name’);
    echo $username;

  3. Comment by Fadi — May 20, 2013 @ 4:38 am

    Hi Markus,


    $user = JFactory::getUser();
    echo ($user->name);

  4. Comment by as — July 14, 2013 @ 9:19 pm

    Thanks for sharing…

    How can we see the submissions of duplicated fields at the backend of RSForm?

  5. Comment by Fadi — July 15, 2013 @ 5:38 pm

    Hi AS,

    You will need to do some further modification to do that. Please contact us and we’ll do it for you.

Leave a comment