8 posts by 2 authors in: Forums > CMS Builder
Last Post: May 21   (RSS)

Hi All,

I must be missing something simple, but try as I might, I can’t get a single value pulldown field in a form to retain its selected value after an error is thrown.

The list field (practice_country) gets it’s list options from the ‘num’ and ‘category’ fields of another database.

The code I’ve got now is below.

Any help appreciated.

Thanks,

Jerry Kornbluth

$country = @$_REQUEST['practice_country']; // in the 'add record if no errors' section at the top of the page. (can't seem to get $country to print after form submission either)

<select name="practice_country" />
<?php foreach ($country_categoriesRecords as $record): ?>

<option value="<?php echo htmlencode($record['category']) ?>"><?php echo htmlencode($record['category']) ?></option>
<?php endforeach; ?>
</select>

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

Hi Jerry,

In order to have a dropdown retain a value, there needs to be some code attached to the <option> elements to indicate which should be selected. We have a helper function called "selectedIf()" for this purpose, which would look something like this:

<?php foreach ($country_categoriesRecords as $record): ?>
  <option value="<?php echo htmlencode($record['category']) ?>" <?php selectedIf($record['category'], $country); ?>><?php echo htmlencode($record['category']) ?></option>
<?php endforeach; ?>

However, you also note that you aren't able to print out $country after form submission, which may indicate an additional problem passing along the form value. If the above doesn't work, can you also add the following (somewhere below the "$country =" assignment) and let me know the output?

<?php var_dump($country); ?>

Thanks,

Daniel
PHP Programmer
interactivetools.com

Hi Daniel,

Sorry for the delay in response. I've been out of the office.

The fix worked perfectly. (I also had the variable defined in the wrong place in my code)

One more issue came up though (don't know why I'm having such difficulty with form fields...)

In my associated profile update form, I’m using the following code to pre-populate a 'country' radio button field that gets it’s values from the contents of records in another table.

Right now it’s showing both hidden and non hidden values, which basically lists all countries. https://dbtproviders.com/provider_profile_test.php

I’d like the list not to show the values for hidden records, but can’t figure out the correct syntax.

(Ultimately I'd rather have a pull down list for this field, but for the cookbook recipe to come, I'd like to know how to do both)

Hope you're still willing to help.

Thanks,

Jerry Kornbluth

<div class="rTableRow">
<div class="rTableCell">Country</div>
<div class="rTableCell">
<?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>
<br />
<?php endforeach ?>
</div>
</div>

to

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

Hey Jerry,

Generally, getListOptions() will get all available options for a list field, so if you want to only show countries that have been selected by non-hidden records, we'll probably need to try a different approach.

First, place this code below the "$fieldname = " assignment:

<?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 the opening of your foreach to look like this:

<?php foreach ($listOptions as $value => $label): ?>

Note that this will only work for single-value list fields, such as radio buttons or single dropdowns.

Try it out and let me know if you have any issues!

Thanks,

Daniel
PHP Programmer
interactivetools.com

Thanks Daniel,

I'm out of town for  few  days but I'll give it try when I return.

Thanks for all your help.

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 gkornbluth - May 21 - edited: May 21

Hi Daniel,

Just got back to the office and I think I'm almost there.

The code you suggested works, but it doesn't display the existing value of the radio button or retain it after an error.

I've tried to implement the selectedIf function above but can't seem to get anywhere.

Thanks as always

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

Hey Jerry,

For radio buttons and checkboxes, you'll need to use checkedIf() instead of selectedIf() - can you try that out? The parameters should be the same for both functions. If that doesn't work, can you paste your latest code here for me to check out?

Thanks,

Daniel
PHP Programmer
interactivetools.com

Thanks Daniel,

I added a checkedIf that I found on the forum to the code above and it now works as planned.

Here's the final code for anyone else who can benefit.

<div class="rTableRow">
<div class="rTableCell">Country</div>
<div class="rTableCell">
<?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' ];
}
}
?>
<?php $idCounter = 0; ?>
<?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>
<br />
<?php endforeach ?>
</div>
</div>

Best,

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