1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

[SOLVED] Form PHP - insert value of checkboxes failed

Discussion in 'Professional Support' started by bea, Jul 21, 2018.

  1. bea

    bea Active Member

    Level: Community
    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::
    var_dump output:
    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:

    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: Jul 21, 2018
  2. troester

    troester Well-Known Member Staff Member

    Level: Community
    Checkbox elements are stored this way
    ["s1","s2"]
    So I think you must do something like
    $security_checks_string = json_encode($security_checks);
     
    bea likes this.
  3. bea

    bea Active Member

    Level: Community
    Many thanks!
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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 (Text):

    $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
     
    bea likes this.

Share This Page