0

I am new to PHP and the project I am working on is a codeigniter php form writing back to a FileMaker database.

The checkboxes should be populating as a list field.

Here is my form

<input name="fieldname[]" value="value1"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value1
<input name="fieldname[]" value="value2"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value2

There are about 7 checkboxes.

I assume that I have to setup a foreach loop to get it to go thought to FileMaker's field, but unsure how to do that.

If I take away the array, the last one selected goes through to the layout.

Any help would be great.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Please include your current code that processes the `$_POST['fieldname']` array. Yes, use a foreach loop. If you don't know how to write a foreach loop it would be better if you deleted this question and went away to do some fundamental research. And finally, we don't know what your intended result is. If by "take away the array" you mean you switch `fieldname[]` with `fieldname` then yes, you will be overwriting values and preserving only the final value. FYI -- Your question risks being closed as Unclear or Too Broad, and may receive downvotes for no coding attempt. – mickmackusa Nov 15 '17 at 03:50
  • Sorry, I have walked into the project and am unsure what part of the `$_POST` to show that is relevant. The entire set of fields(around 150) are placed in an array, FileMaker commands send through and the data placed in the appropriate fields. As for the `foreach` loop, I am unsure the best way to concatenate all the checkbox options into a list. I'm sure if I knew how to do that, I can figure out how to send it to the layout. – Pierre Odendaal Nov 15 '17 at 20:13
  • I am sure that we volunteers can help you, but only if you give us all of the pieces of the puzzle. We need to see a minimal, relevant representation of the input, your coding attempt, and your expected outcome. Help us to help you by improving the quality of your question. I assume `$_POST['fieldname']` elements will suffice. Show us what you are trying to do with them. We repair code, not write new code. – mickmackusa Nov 15 '17 at 20:30

3 Answers3

0

Presumably, these values mirror a valueList back in FileMaker. If so, it's best to assign the valueList to a variable:

$values = $layout->getValueList('pizzaToppings');

And use that in your for loop:

foreach($values as $value)...

In your if(isset()) block, post a return-delimited array to FileMaker:

// value(s)!
if ($_POST['fieldname']) {
    $valueArray = implode("\r", $_POST['fieldname']);
}

// pass the array to setField()
$record->setField('filemakerField', $valueArray);
Brian Hamm
  • 426
  • 3
  • 8
0

I worked it out. To enter checked boxes into a return separated list:

View:

At the top of the section with the checkboxes

<?php $join->fieldname = explode("\n", $join->fieldname); ?>

// The actual input in the form
<input name="fieldnameXX[checkboxValue]" value="value" <?php echo (in_array('value', set_value('fieldname[value]', $join->fieldname)) ? ' checked="checked"': '')?> type="checkbox" class="form-check-input" > Value

Factory:

$fieldnameZZ = $data['fieldnameXX'];
$data['fieldnameXX'] = FALSE;
unset($data['fieldnameXX']);
$sacraments = implode("\n", $fieldnameZZ);
$data['fieldname'] = $fieldnameZZ;

I'm not sure if this is the best way to have done it, but it works.

-1
    $ids=implode(",", $_REQUEST["fieldname"]);
    $result3=mysqli_query($dbh,'SELECT* FROM excel_tenant WHERE ID IN ("' . 
    $ids .    '") AND
    ManagerID =  "'.$_SESSION["ManagerID"].'" ORDER BY ID DESC ') or 
    die(mysqli_error($dbh));
  • Please read about [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – nvoigt Nov 15 '17 at 09:01