Working with Checkboxes

Checkboxes are a great way to store more than one value in a single field. FileMaker makes it easy to format fields as checkboxes — select the field, invoke the Field/Control Setup dialog box, select the display style and associate a value list. The user sees clickable checkboxes but FileMaker is actually storing the values as a paragraph-separated list.


It's not as easy to work with checkboxes on the web because you need to assemble the values three different ways depending on what you're trying to achieve:

  • a paragraph-separated stack for saving to FileMaker
  • a comma-separated string to display nicely on a page
  • an array to show as editable checkboxes

From FileMaker to Display Page

Here's how to take a paragraph-separated list from the database and display it as a comma-separated list on the web. First, set the field value to a variable.

$coreStack = $record->getField('rt_CoreRequirement'); 

Next, convert the paragraph-separated stack to a comma separated string using str_replace.

$coreString = str_replace ("\n", ", ", $coreStack );

Display on your page with echo.

echo $coreString //displays "Historical Perspectives, Mathematical Thinking"

From FileMaker to Editable Checkboxes

Here's how to take a paragraph-separated list from the database and display it as checkboxes that the user can edit. Just like above, set the field value to a variable.

$coreStack = $record->getField('rt_CoreRequirement'); 

Next, convert the paragraph-separated stack to an array using explode.

$coreArray = explode("\n", $coreStack);

Now, use the array to test each value in the value list. In this example, the value list for core requirements was called at the top of the page using these two lines:

$layout =& $fm->getLayout('web_CourseEdit');
$coreValues = $layout->getValueListTwoFields('CoreList');

Here's the chunk of code followed by what the browser renders, followed by what it looks like on screen. Then we'll break it down.

<label>Core Requirements</label><br />
$coreRep = 0; 
foreach($coreValues as $value){ 
$coreRep = $coreRep + 1; ?>
<input name="core<?php echo $coreRep; ?>" type="checkbox" 
value="<?php echo $value; ?>" id="<?php echo $value; ?>" 
<?php if(in_array($value, $coreArray)) { echo "CHECKED"; }?>>
<label for="<?php echo $value; ?>"><?php echo $value; ?></label><br />
<?php } ?>
<input name="coreRep" type="hidden" value="<?php echo $coreRep; ?>"/>

<label>Core Requirements<br />
<input name="core1" type="checkbox" value="Biological Science with Lab" id="Biological Science with Lab" ><label for="Biological Science with Lab">Biological Science with Lab</label><br />
<input name="core2" type="checkbox" value="Physical Science with Lab" id="Physical Science with Lab" ><label for="Physical Science with Lab">Physical Science with Lab</label><br />
<input name="core3" type="checkbox" value="Historical Perspectives" id="Historical Perspectives" CHECKED><label for="Historical Perspectives">Historical Perspectives</label><br />
<input name="core4" type="checkbox" value="Social Sciences" id="Social Sciences" ><label for="Social Sciences">Social Sciences</label><br />
<input name="core5" type="checkbox" value="Literature" id="Literature" ><label for="Literature">Literature</label><br />
<input name="core6" type="checkbox" value="Arts/Humanities" id="Arts/Humanities" ><label for="Arts/Humanities">Arts/Humanities</label><br />
<input name="core7" type="checkbox" value="Mathematical Thinking" id="Mathematical Thinking" CHECKED><label for="Mathematical Thinking">Mathematical Thinking</label><br />
<input name="coreRep" type="hidden" value="7"/>

These lines create a counter for each value in the value list.

$coreRep = 0; ...
$coreRep = $coreRep + 1;

The counter is used to serialize the input field for each checkbox. This will help us process the data on the response page.

<input name="core<?php echo $coreRep; ?>" type="checkbox" ...

To determine whether the box is checked, use in_array. It tests whether the needle ($value) is in the haystack ($coreArray).

...<?php if(in_array($value, $coreArray)) { echo "CHECKED"; }?>>

Finally, tell the response page how many input fields to process.

<input name="coreRep" type="hidden" value="<?php echo $coreRep; ?>"/>

For more on radio buttons, checkboxes and drop-down lists, read, "Custom Web Publishing: Add Elements to Search Form." To understand how ids and labels work, read, "Custom Web Publishing: Accessible Forms."

From Editable Checkboxes to FileMaker

On the response page, build an array of each value the user CHECKED using while.

$coreRep = $_POST['coreRep'];
$coreCount = 0;
while ($coreCount <= $coreRep) {
$coreCount = $coreCount + 1;
if(!empty($_POST['core'.$coreCount])) { 
$coreArray [] = $_POST['core'.$coreCount]; 

Now turn the array into a paragraph-separated stack using implode.

$coreStack = implode("\r", $coreArray); 

And that, my friends, is what you save to the database.

$editCourse->setField('rt_CoreRequirement', $coreStack);