PRE-POPULATING RADIO BUTTONS IN FORMS FROM A MASTER LIST - May 28th, 2019
|
When used in a form, radio buttons allow only one value to be chosen from a list of values.
The trick here was to populate the list of values from the values available in separate master list, and use that list to populate a radio button list field in the accounts section (some of the functions in this example require the Website Membership plugin).
Here’s are the steps...
1) Create a multi-record section (called “my_values”) with a “hidden’ check box and a text field called “contents”
2) In the accounts section, create a radio button list field (called my_list_field) that gets its options from the MySQL query:
SELECT num, contents FROM `<?php echo $TABLE_PREFIX ?>my_values` WHERE hidden = 0 ORDER BY dragSortOrder ASC ;
3) Near the top of the viewer that contains your form, after the load records calls, add the following variable to the list of variables:
@$my_list_field = @$_REQUEST['my_list_field'] ;
4) After any error checking include the following in your $colsToValues array
@$colsToValues['my_list_field'] = $_REQUEST['my_list_field'] ;
5) to pre-populate your form from current user values in the accounts table
// pre-populate form with current user values foreach ($CURRENT_USER as $name => $value) { if (array_key_exists($name, $_REQUEST)) { continue; } $_REQUEST[$name] = $value; }
5) In your form, to show a radio button list of all values that are not hidden in the "my_values" section, use the code:
<tr> <td valign="top">My Available Values</td> <td> <?php $fieldname = 'your_list_field'; ?> <?php $idCounter = 0; ?> <?php foreach (getListOptions(accountsTable(), $fieldname) as $value => $label): ?> <?php $id = "$fieldname." . ++$idCounter; ?> <input type="radio" name="<?php echo $fieldname ?>" id="<?php echo $id ?>" value="<?php echo htmlencode($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?> /> <label for="<?php echo $id ?>"><?php echo htmlencode($label) ?></label> <?php endforeach ?> </td> </tr>
Another Approach (shows all values):
<?php $fieldname = 'practice_country'; ?> <?php $idCounter = 0; ?> <?php foreach (getListOptions(accountsTable(), $fieldname) as $value => $label): ?> <?php $id = "$fieldname." . ++$idCounter; ?> <input id="<?php echo $id ?>" name="<?php echo $fieldname ?>" type="radio" value="<?php echo htmlencode($value) ?>" /> <label for="<?php echo $id ?>"><?php echo htmlencode($label) ?></label>
<?php endforeach ?>
<b>To show only values that are not hidden</b>: Since getListOptions() will get all available options for a list field, if you want to only show countries that have been selected by non-hidden records, here's a different approach.
<?php $fieldname = 'practice_country'; ?> <?php $tablename = accountsTable(); ?> <?php // get all table records list($tableRecords, $tableRecordsMeta) = getRecords(array( 'tableName' => $tablename, 'loadUploads' => false, 'loadCreatedBy' => false, 'allowSearch' => false, )); // group records by target field to find option keys $tableRecordsByField = array_groupBy($tableRecords, $fieldname); $keys = array_keys($tableRecordsByField); // create option list $listOptions = []; foreach($keys as $key) { if (!empty( $key )) { $listOptions[ $key ] = $tableRecordsByField[ $key ][ $fieldname.':label' ]; } } ?>
This will create the variable $listOptions that should contain all of the currently in-use list values and labels for the target table/field. This should let you then update your foreach to look like this:
<?php foreach ($listOptions as $value => $label): ?> <?php $id = "$fieldname." . ++$idCounter; ?> <input id="<?php echo $id ?>" name="<?php echo $fieldname ?>" type="radio" value="<?php echo htmlencode($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?>/> <label for="<?php echo $id ?>"><?php echo htmlencode($label) ?></label> <?php endforeach ?>
Note that this will only work for single-value list fields, such as radio buttons or single dropdowns.
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.