Checkboxes acting strange

Hello.  this is my first time posting, but I'll try to be thorough with my question.  I've been developing a module with a form for it, and the form has checkboxes.  The first time that someone submits the form and checks on a checkbox, it works correctly (or what I think is correctly).  It sends the string "on" for the value of that field.  When I go to edit the information, the checkbox is no longer checked, but if i dump the form, the value is still "on".  If I submit the form again, it erases the "on" from attribute in the database.  This makes sense since the form is sending the current inputs and the checkbox is not checked.  I wrote the logic to check a checkbox when the value returned from the database for the field is "on".  That is fixed.

The problem is that after the initial submit, any time I try to edit the information, I have to submit it twice (clicking on the checkbox each time) for it to send the information to the database.  I used Chrome's Developer Tools to see what was being sent.  The first time (after the initial submit) that the form is submitted, the name of the checkbox appears in the POST data, but no value.  The second time it has the name and "on" as the value.  

Is something strange happening, or am I doing something wrong?  Below are a few code snippets of the "problem" areas.

The form item:

 'management' => array(

                    'type' => 'checkbox',

                    'name' => 'management',

                    'caption' => "Management Experience?",

                ),

Here is the edit action
 
 function actionEdit($id) {
        if(!$GLOBALS['logged']['member']) {
            $this->_oTemplate->displayAccessDenied();
            return;
        }
        $aResult = $this->_oDb->getRow("SELECT * FROM my_skillset WHERE id = ". $id);
        $this->_oTemplate->pageStart();
        $oForm = new BxTemplFormView($this->aForm);
        $oForm->initChecker($aResult);
        if($oForm->isSubmittedAndValid()) {
            //$iResult = $oForm->insert(array('employerid' => getLoggedId()));
            $oForm->update($id);
            var_dump($this->_oDb->getRow("SELECT * FROM syncsum_emp_pos WHERE id = ". $id));
          echo $oForm->getCode();
        } else {
          echo $oForm->getCode();
        }
        $this->_oTemplate->pageCode(_t("Edit Skillset"), true);
    }
 
I have been racking my brain on this one for a few days, so any help would be greatly appreciated!
Quote · 6 Feb 2014

A couple of things that I noticed. For one - to get the chckbox checked when returning to the form, you need to have the code in place that will display the original results. And second, when returning to the form, you are using another insert to input the data, and not an update to change existing data. Dolphin does have a way to use the same form for update and insert, but you still have to specify which one you would use. It is a lengthy explanation (at least for me it is) so I will do my best to explain.

 

I tend to do things the long way, so heres what I did. Create 2 different forms - One for Insert and one for update. Like so:

Insert ---

       $aForm = array(
             'form_attrs' => array(
                 'name'     => 'form_my',
                 'method'   => 'post',
             ),
 
             'params' => array (
                 'db' => array(
                     'table' => 'module_table', // table name
                     'key' => 'module_table_id', // key field name
                     'uri' => 'module_table_uri_field', // uri field name
                     'uri_title' => '', // title field to generate uri from
                     'submit_name' => 'submit_form',
                 ),
                 'csrf' => array(
                       'disable' => true,
                 )
               ),
 
             'inputs' => array(
 
                'header1' => array(
                    'type' => 'block_header',
                    'caption' => _t('_language key'),
                ),   
                 'checkbox_field' => array(
                     'type' => 'checkbox',
                     'name' => 'checkbox_field',
                     'value' => $checkbox_field,
                     'caption' => _t('_language key'),
                     'required' => true,
                     'checker' => array (
                         'func' => 'avail',
                         'error' => _t('_language key'),
                     ),
                     // database params
                     'db' => array (
                         'pass' => 'Xss', 
                     ),
                 ),
                'header1_end' => array(
                    'type' => 'block_end'
                ),
                'submit' => array(
                    'type' => 'submit',
                    'name' => 'submit_form',
                    'value' => _t('_Submit')
                )
                 ),
             );
         $oForm = new BxTemplFormView ($aForm);
         $oForm->initChecker();
         
         
        if ($oForm->isSubmittedAndValid()) {
        $aValsAdd = array (
             );
            if ($oForm->insert($aValsAdd )) {
               
                $sActionKey = '_success';
            } else {
                $sActionKey = '__fail';
            }
            $sActionText = MsgBox(_t($sActionKey), 3);
        } else {
        echo $oForm->getCode();
        }

 

and one for update:

        $pSomething = mysql_query("SELECT * FROM `table` WHERE `table_field` = '$some variable'");
        while ($row = mysql_fetch_array($pSomething)) {
            $checkbox_field = $row["checkbox_field"];
        }
      
       $aForm = array(
             'form_attrs' => array(
                 'name'     => 'form_my',
                 'method'   => 'post',
             ),
 
             'params' => array (
                 'db' => array(
                     'table' => 'module_table', // table name
                     'key' => 'module_table_id', // key field name
                     'uri' => 'module_table_uri_field', // uri field name
                     'uri_title' => '', // title field to generate uri from
                     'submit_name' => 'submit_form',
                 ),
                 'csrf' => array(
                       'disable' => true,
                 )
               ),
 
             'inputs' => array(
 
                'header1' => array(
                    'type' => 'block_header',
                    'caption' => _t('_cf_memberships_aim_header', $Name, $Description, $Price, $Unit),
                ),   
                 'checkbox_field' => array(
                     'type' => 'checkbox',
                     'name' => 'checkbox_field',
                     'value' => $checkbox_field,
                     'caption' => _t('_language key'),
                     'required' => true,
                     'checker' => array (
                         'func' => 'avail',
                         'error' => _t('_language key'),
                     ),
                     // database params
                     'db' => array (
                         'pass' => 'Xss', 
                     ),
                 ),
                'header1_end' => array(
                    'type' => 'block_end'
                ),
                'submit' => array(
                    'type' => 'submit',
                    'name' => 'submit_form',
                    'value' => _t('_Submit')
                )
                 ),
             );
         $oForm = new BxTemplFormView ($aForm);
         $oForm->initChecker();
        if ($oForm->isSubmittedAndValid()) {
        $aValsAdd = array (
             );
            if ($oForm->update ($module_table_id, $aValsAdd )) {
               
                $sActionKey = '_success';
            } else {
                $sActionKey = '_fail';
            }
            $sActionText = MsgBox(_t($sActionKey), 3);
        } else {
        echo $oForm->getCode();
        }

 

I know there is a way to only have the one form and then just calling the form when needed, but not knowing exactly where you are putting this code, I can't tell you exactly where to put the form. If you want more details, PM me.

caredesign.net
Quote · 6 Feb 2014
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.