4

I have created a module by module creator. I have added checkbox code in Form.php from Block/Adminhtml/modulename/edit/tab/form.php.

$fieldset->addField('title', 'checkboxes', array(
    'label'     => Mage::helper('custommodule')->__('Title'),
    'name'      => 'title',
    'values' => array(
        array('value'=>'aaa','label'=>'Checkbox1'),
        array('value'=>'bbb','label'=>'Checkbox2'),
        array('value'=>'ccc','label'=>'Checkbox3'),
    ),
    /* 'checked' => array('aaa','bbb','ccc'), */
    'onclick' => "",
    'onchange' => "",
    'disabled' => false,
    'tabindex' => 1
));

Now How should I add this checkbox values to Database and Vice Versa how to assign value to checkbox while editing.

Thanks in advance

5
  • Is it your own code or copied from somewhere? Just asking.... Jul 9, 2014 at 12:48
  • this is my code. Right now I am working on it.
    – YKJ
    Jul 9, 2014 at 13:13
  • I got successful in adding checkbox value to database as well as I got successful to fetch data on edit.php page too. Right now the issue is only to assign that array value to Checkbox field.
    – YKJ
    Jul 9, 2014 at 13:17
  • My advice: better use yes/no
    – zhartaunik
    May 8, 2015 at 13:37
  • I see you're setting the value by using the 'checked' attribute (althoug commented out now). It might be that the values are reset later on in the code by $form->setValues($formValues) Oct 15, 2015 at 18:46

2 Answers 2

1

You need to add this code with valid param which are you using to make a checkbox in admin. by this you can get the value for every check box checked in admin form.

$params = Mage::app()->getRequest()->getParam('id');
if ($params != NULL) {
    $warehouses = Mage::getModel('NamespaceName_ModuleName/collect')->load($params);
    $getcheckbox = $warehouses['checkbox'];
    //echo "<pre>";print_r($getcheckbox);exit;
    $baseurl = Mage::getBaseUrl();
    if (strstr($baseurl, 'index.php')) {
        $baseurl = str_replace('/index.php', '', $baseurl);
    }
    ?>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type=text/javascript>
        jQuery(document).ready(function () {
            var access = '<?php echo $getcheckbox; ?>';
            var myacess = access.split(",");
            var i;
            for (i = 0; i < myacess.length; i++) {
                jQuery('#checkbox_' + myacess[i]).attr('checked', 'checked');
            }
        });
    </script>
<?php } ?>

Hope this will help you.

1

Follow bellow steps

Step : 1 Add code in (Block/Adminhtml/modulename/edit/tab/form.php)

$model = Mage::registry('modulename_data');
$fieldset->addField('title[]', 'checkboxes', array(
            'label'     => Mage::helper('salesman')->__('Title'),
            'name'      => 'title[]',
            'values' => array(
                array('value'=>'aaa','label'=>'Checkbox1'),
                array('value'=>'bbb','label'=>'Checkbox2'),
                array('value'=>'ccc','label'=>'Checkbox3'),
            ),
            'checked' => unserialize($model->getTitle())
        ));

Step : 2 Add code in admin controller (ModuleNamespace\modulename\controllers\Adminhtml\YourcontrollernameController.php)

if ($data = $this->getRequest()->getPost()) {
    $data['title'] = serialize($data['title']);
    ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.