[SOLVED] Form PHP - insert value of checkboxes failed

bea

Active Member
Hi,
I've a problem to get the value of check-boxes (4) with form PHP into the next record with insert query.

PHP:
$security_checks =$formModel->getElementData('fab_cp1_line_tour___security_checks',true);
Error message::
Unknown column 'Array' in 'field list'
var_dump output:
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }

PHP:
$security_checks =$formModel->getElementData('fab_cp1_line_tour___security_checks',true);
$security_checks=is_array($security_checks) ? $security_checks[0] : $security_checks;
Now only first check appears in next record. Others are ignored. var_dump output:
string(1) "1"


All other elements, like dropdown, joins, fields work correct:
PHP:
$security_status =$formModel->getElementData('fab_cp1_line_tour___security_status',true);
$security_status=is_array($security_status) ? $security_status[0] : $security_status;
PHP:
if($record[0] == 1 && $status[0] == 2  ){
$newstatus=  '1';
$newrecord = '1';
$query=$mydb->getQuery(true);
$query->insert('fab_cp1_line_tour')
->set('machine = '.$mydb->quote($machine))
->set('status = '.$mydb->quote($newstatus))
->set('record = '.$mydb->quote($newrecord))
->set('security_status = '.$mydb->quote($security_status))
->set('security_checks = '.$mydb->quote($security_checks));
$mydb->setQuery($query);
$mydb->execute();
$id=$mydb->insertid();
}

Cheers,
Bianka
 
Last edited:
Checkbox elements are stored this way
["s1","s2"]
So I think you must do something like
$security_checks_string = json_encode($security_checks);
 
  • Like
Reactions: bea
Note that depending on whether you "do something" to the $security_checks array, you may need to do an array_values() on it before json_encode()'ing it. Specifically, if you do anything that might alter the key values or sequence, for example by removing an array entry. If you do that, such that they keys no longer start with 0 and increase sequentially by 1, json_encode() will add the keys ...

["1":"s1","3":"s3"]

... which will mess with Fabrik's head when it next reads them. So it's usually safest to do ...

Code:
$security_checks_string = json_encode(array_values($security_checks));

The array_values() ensures that the array is re-indexed, starting at 0, and json_encode() will then output the format we expect, ["s1","s2"], with no keys.

-- hugh
 
  • Like
Reactions: bea
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top