How to Automatically Populate Dropdown Fields from a Previous Form's Submissions

In this article, we’ll show how selecting an option from a dropdown and entering text in a field in the first form (let's name it 'Add Value Form') can automatically populate a dropdown (or a Selectize field) in the second form (let's name it 'Show Values Form') after submission. For example, if you choose 'Dropdown2' in the 'Select_Field' and enter 'text value 2' in the 'Value' field, in the 'Add Value Form', then submit it, the 'Dropdown2' field in the 'Show Values Form' will include this newly submitted value, see screenshot below.

 

In the 'Add Value Form', we will need:

  • A dropdown field, let's name it, Select_Field, with the following items:
    • Dropdown1
    • Selectize1
    • Dropdown2
    • Selectize2
  • A textbox field, let's name it Value
 

In the 'Show Values Form' we will need:

  • 4 dropdown fields, or two regular dropdowns named Dropdown1 and Dropdown2, and two Selectize fields named Selectize1 and Selectize2.

Each of these 4 fields will include the following code in the Items area:

//<code>
// Change this to your own ID
$showValuesFromFormId = 1;
// Change this to your own field name
$selectFieldName = 'Select_Field';
// Change this to your own field name
$valueFieldName = 'Value';

// Prepare the empty array
$items = array();

// Prepare the database connection
$db = Joomla\CMS\Factory::getDbo();

// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";

require_once JPATH_ADMINISTRATOR . '/components/com_rsform/helpers/submissions.php';

$db->setQuery("SELECT SubmissionId FROM #__rsform_submissions WHERE FormId = " . $db->q($showValuesFromFormId));
if ($results = $db->loadColumn())
{
  foreach ($results as $result)
  {
    $submission = RSFormProSubmissionsHelper::getSubmission($result);
    if (isset($submission->values[$selectFieldName]) && $submission->values[$selectFieldName] == $this->name)
    {
      $items[] = $submission->values[$valueFieldName];
    }
  }
}

// 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
return $items;
//</code>

You will need to make sure that you're replacing:

  • $showValuesFromFormId = 1; with your 'Add Value Form' form ID.
  • $selectFieldName = 'Select_Field'; with your 'Add Value Form' dropdown field name.
  • $valueFieldName = 'Value'; with your 'Add Value Form' textbox field name.
 

You can use any number of Dropdown / Checkbox Group / Radio Group or any other field that has pre-defined values.

 

Was this article helpful?

Yes No
Sorry about that