Adding MultiValue fields to a php form outside of CMS

12 posts by 3 authors in: Forums > CMS Builder
Last Post: April 7, 2014   (RSS)

By nmsinc - March 24, 2014

I'm building an application form to allow user registration outside of CMS. I have a field that contains Multi-Values and I need to pass these values to this form with a check box for each value. I need the user to be able to check multiple boxes just like in the CMS for as many values he/she wants. I also need help with the code to pass these checked and unchecked values via 'set' into the database!

Thanks - nmsinc

nmsinc

By Daryl - March 28, 2014 - edited: March 31, 2014

Hi nmsinc,

You can generate a check box lists from a multi-value fields from a section using getListOptions() function. This function will return an array of value=>label of a list field. For example:

$multiValueFieldValuesToLabels = getListOptions($tableName, 'multiValueFieldName');
foreach ($multiValueFieldValuesToLabels as $value => $label){
  echo '<input type="checkbox" name="yourCheckboxName[]" value="'.htmlspecialchars($value).'">'.htmlspecialchars($label).'<br />';
}

Notice that the checkbox name have an open and close square brackets. Adding square brackets at the end of a check box field name will store the data in an array when the form is submitted. In the example above, we're going to have and array values of the checked checkbox for this request variable:

if (@$_REQUEST['submitForm']){
  showme(@$_REQUEST['yourCheckboxName']);
}

The multi-value list field stores data in a form of "tab-separated values" (TSV) so in order to store the checkboxes values, you need to implode the array values with "tabs" as the glue string, for example:

if ( @$_REQUEST['yourCheckboxName']){
  $colsToValues['multiValueFieldName'] = "\t" . implode("\t", $_REQUEST['yourCheckboxName']) . "\t";
}

Hope this helps!

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

By nmsinc - March 29, 2014

Hi Daryl,

The form I'm creating outside the CMS needs to populate with all values checked or unchecked so that the user can uncheck or check any value and save to the 'account' section table - the code you provided would not work.

I need to grab all multi values for the form from the field labeled "experience" located in the 'accounts' section table which populates via an advance filter with a multi value choices from a section labeled 'claim_types' - see code I'm using to populate the 'experience' field within the "accounts" section below.

SELECT num, title
FROM `<?php echo $TABLE_PREFIX ?>claim_types`
WHERE `company` = '<?php echo $ESCAPED_FILTER_VALUE ?>'

Also I use a query set to save the data from the form to 'accounts' and tried to add the implode suggestion in blue below with no luck so I'm going to need a little more help with this too!

$colsToValues['experience'] = implode("\t", $_REQUEST['experience']);


mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
                      
                      latitude                           = '".mysql_escape( $lat )."',
                      longitude                          = '".mysql_escape( $lng )."',
                      fullname                           = '".mysql_escape( $_REQUEST['fullname'] )."',
                      address                            = '".mysql_escape( $_REQUEST['address'] )."',
                      city                               = '".mysql_escape( $_REQUEST['city'] )."',
                      state                              = '".mysql_escape( $_REQUEST['state'] )."',
                      zip                                = '".mysql_escape( $_REQUEST['zip'] )."',
                      phone                              = '".mysql_escape( $_REQUEST['phone'] )."',
                      cell                               = '".mysql_escape( $_REQUEST['cell'] )."',
                      email                              = '".mysql_escape( $_REQUEST['email'] )."'
                      username                           = '".mysql_escape( $_REQUEST['username'] )."',
                      password                           = '".mysql_escape( $passwordHash )."',
                      
                      experience                         = '".mysql_escape( $colsToValues['experience'] )."',

                      member_company_accounts            = '".mysql_escape( $_REQUEST['member_company_accounts'] )."',
                      user_type                          = '".mysql_escape( $_REQUEST['user_type'] )."',
                      company_type                       = '".mysql_escape( $userTypeRecord['member_type'] )."',
                      send_email_to_users                = '".mysql_escape( $_REQUEST['send_email_to_users'] )."',
                      import_record_type                 = '".mysql_escape( $_REQUEST['import_record_type'] )."',
                      assigned_to                        = '".mysql_escape( $_REQUEST['assigned_to'] )."',
                      disabled                           = '0',
                      isAdmin                            = '0',
                      expiresDate                        = '0000-00-00 00:00:00',
                      neverExpires                       = '1',
                      createdDate                        = NOW(),
                      updatedDate                        = NOW(),
                      createdByUserNum                   = '".mysql_escape( $CURRENT_USER['num'] )."',
                      updatedByUserNum                   = '0',
                  
                      date_updated                       = '".mysql_escape( $_REQUEST['date_updated'] )."',
                      
                      
                      name_of_insured              = '".mysql_escape( $_REQUEST['name_of_insured'] )."'")
      or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

Thanks - nmsinc

nmsinc

By Chris - March 31, 2014 - edited: March 31, 2014

Hi nmsinc,

I want to make sure I understand what you're asking before I respond. Do you need the list of checkboxes to change dynamically depending on the selection a user had made in another field? Such as if someone changes the Company dropdown, the checkboxes all disappear and are replaced with a new set of checkboxes?

P.S. Also, you'll need to add a tab before and after the tab-separated list.

$colsToValues['experience'] = "\t" . implode("\t", $_REQUEST['experience']) . "\t";

All the best,
Chris

By nmsinc - March 31, 2014

HI Chris,

The multi value choices are located in a CMS section labeled "accounts" with a field label of "experience'. I need the multi value choices within the 'experience" field to propagate to a non CMS form (already built) just like it would if it were in the CMS .

Each choice would have a Check Box followed by the Selection Label.

Any value(s) that had been previously checked within the CMS section should also be checked within this form during population. The user can then check or uncheck any value and submit these changes to the 'accounts' section.

Hope I have been a little more clear on my request - sorry!

Thanks - nmsinc

nmsinc

By Chris - March 31, 2014 - edited: March 31, 2014

Ahh, thank you for clearing that up. I think the only problem with the code that Daryl supplied is that the checkboxes need to start out as checked if the record being edited has them selected in the list field.

Is the record data available in $_REQUEST? If not, please post the complete source code of your form as an attachment so I can see what variables you're using.

If you're setting $_REQUEST to the record, I'm guessing something like this should work. New code in blue. Variable containing the current value of the multi-value field in red.

$myExperiences = @$_REQUEST['experience'];
if (!is_array($myExperiences)) {
  $myExperiences = coalesce(@explode("\t", trim($myExperiences)), array());
}
$multiValueFieldValuesToLabels = getListOptions($tableName, 'multiValueFieldName');
foreach ($multiValueFieldValuesToLabels as $value => $label) {
  $isChecked   = in_array($value, $myExperiences);
  $checkedAttr = $isChecked ? 'checked="checked"' : '';
  echo '<input type="checkbox" name="yourCheckboxName[]" value="'.htmlspecialchars($value).'" $checkedAttr>'.htmlspecialchars($label).'<br />';
}

Does that help?

All the best,
Chris

By nmsinc - April 2, 2014

Hi Chris,

I'm still not able to get this working - can you give a secure channel to upload this file as this venue is not secure!

Thanks - nmsinc

nmsinc

By nmsinc - April 6, 2014

I sent a request on Thursday, however I will provide the error below that I'm receiving:

Warning: Missing argument 2 for getListOptions(), called in /home/claimsca/public_html/user-profile-admin-update.php on line 1221 and defined in /home/claimsca/public_html/cmsAdmin/lib/database_functions.php on line 486 loadSchema: tableName ' Fire Damage Flood Damage Hail and Wind Damage Wind and Rain Damage Water Damage ' contains invalid characters!

Here is the code:

<!-- 47 -->
    <?php $companyRecord = mysql_get('member_companies', $CURRENT_USER['member_company_accounts']); ?>
    <?php if ($companyRecord['adjuster_experience_sorting'] == "1"): ?>
    <tr>
    <td align="left" width="234" bgcolor="#FFFFFF" valign="top">
 <font face="Verdana" size="1">Set user experience for the following claims types
    </font>
    </td>
    <td align="left" width="490" bgcolor="#FFFFFF" valign="top">
    <font size="2" face="Verdana">
    <?php
    $myExperiences = @$_REQUEST['experience'];
    if (!is_array($myExperiences)) {
    $myExperiences = coalesce(@explode("\t", trim($myExperiences)), array());
    }
    $multiValueFieldValuesToLabels = getListOptions($accountsRecord['experience']);
    foreach ($multiValueFieldValuesToLabels as $value => $label) {
    $isChecked   = in_array($value, $myExperiences);
    $checkedAttr = $isChecked ? 'checked="checked"' : '';
    echo '<input type="checkbox" name="experience[]" value="'.htmlspecialchars($value).'" $checkedAttr>'.htmlspecialchars($label).'<br />';
    }
    ?>
    </font>  
 </td>
    </tr>
    <?php endif; ?>

Thanks - nmsinc

nmsinc

By Chris - April 7, 2014

Hi nmsinc,

You must supply the tableName and fieldName to getListOptions(). I think you're going to want to replace this line:

$multiValueFieldValuesToLabels = getListOptions($accountsRecord['experience']);

...with this:

$multiValueFieldValuesToLabels = getListOptions('accounts', 'experience');

Taking a look at your page via FTP, fixing this for you may be a little too complicated for our free support service. I'll do what I can, but please know that I'm already going a little above and beyond what we're supposed to offer for free. Our consulting department is always available to help, of course. Please let me know if you want any help getting connected with them.

Please try out the above change and let me know if you get any more errors and I'll do what I can to help!

All the best,
Chris