Dynamic form fields

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 6, 2018   (RSS)

Hi All,

I’d like to create dynamic sets of fields in a front end form which allows a group administrator to list as many members of their group as needed, and on form submission, enter that information into the group’s user record.

My current (but not very elegant) approach is to create a dozen sets of fields in a user record (Ideally I'd like to use a dynamic scheme on the back end too), like:
member_name_1
member_description_1
member_name_2
member_description_2
member_name_3
member_description_3
member_name_4
member_description_4
etc.

I’m then allowing the group admin to populate these fields with a front end form. The form uses an if statement which only shows these fields if the admin has checked a box on their membership application indicating that there are multiple members in the group.

I’ve seen a number of scripts that use jquery Ajax to add forms fields dynamically and create arrays to add data to a database, like the one at: https://www.youtube.com/watch?v=kBAPbCDGCuY but I don’t have a clue how I’d implement the concept in CMSB to insert the data into an existing account record, update the data by another front end form, and ultimately display the data as a search result, etc.

Anyone done something like this before?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - November 6, 2018 - edited: November 6, 2018

Hi Jerry,

I can think of a couple ways this could be done; it's a bit more complex than what I can fully walk you through on the forum, but I'll try to give you some ideas/somewhere to start at least.

One of the simpler methods to do this would be by encoding all of the data into a single value, which means that you can save a dynamic number of records to a single field. This can be done by passing an array of information into json_encode(). So, assuming you've done something like in that YouTube tutorial and you've named the inputs member_name[] and member_description[], it could look something like this:

  ...

  $member_data         = array();
  $member_names        = isset( $_REQUEST['member_name'] )        ? $_REQUEST['member_name']        : array();
  $member_descriptions = isset( $_REQUEST['member_description'] ) ? $_REQUEST['member_description'] : array();
  
  if (!empty( $member_names ) && !empty( $member_descriptions )) {
    foreach ($member_names as $key => $name) {
      $member_data[] = array(
        'name'        => $name,
        'description' => $member_descriptions[ $key ],
      );
    }
  
    $member_data_json = json_encode( $member_data );
    $colsToValues['member_data'] = $member_data_json;
  }
  
  ...

You could then use json_decode() to reverse the process and obtain an array of the data. It would be up to you to determine how you need to use that data, but here's an example of how it could be output in a list.

<?php
  $member_data = json_decode($exampleRecord['member_data'], true);
  foreach ($member_data as $member) {
    echo 'Name: ' . $member['name'] . '<br>';
    echo 'Description: ' . $member['description'] . '<br>';
    echo '<hr>';
  }
?>

The downside to this method is that it makes it difficult to directly view/edit the data through the CMSB back-end, as it would only be visible as the encoded data there.

Another idea could be to create a separate CMSB section for this member data and create a new record for each name/description pair, using an "ownerNum" field (or similar) to keep track of which user they belong to. This provides a better interface for accessing the data through CMSB but requires quite a bit more work to set up, especially when trying to update/delete any of the records.

Let me know if you have any questions!

Thanks,

Daniel
Technical Lead
interactivetools.com