Question

I want to add single check box in my extension like below.

enter image description here

Please check my system.xml

<config>
    <sections> 
      <mymodule>
            <groups>
                <general translate="label" module="paygate">
                    <label>General settings</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                      <visible_on_frontend translate="label">
                        <label>Visible on Front-End</label>
                        <frontend_type>checkbox</frontend_type>
                        <frontend_model>mymodule/system_config_form_frontendavail</frontend_model>
                        <sort_order>22</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>0</show_in_store>
                    </visible_on_frontend>
                  </fields>
                </general>
            </groups>
      </mymodule>
  </sections>
</config>

Frontendavail.php

<?php
class Namspace_Mymodule_Block_System_Config_Form_Frontendavail extends Mage_Adminhtml_Block_System_Config_Form_Field  
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $html = "
           <input type='checkbox' class='required-entry' name='terms' value='1'>";
        return $html;
    }
}

After doing this check box is display but nothing is happens. Not saving data in database.

Was it helpful?

Solution

Not sure about this but your input name is wrong.
It should be in this format groups[general][fields][terms][value].
That structure is needed in order to read the values properly and store them in the database.
[EDIT]

Checkboxes are a bit strange.
If you don't check them, no value is sent via post and the way the system configuration section works for magento it will ignore the not checked field.
You should try as much as possible to avoid checkboxes. Use yes/no selects instead.
But if you really want to use the checkbox, me your _getElementHtml method look like this:

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
    $html = "
       <input type='hidden' name='groups[general][fields][terms][value]' value='0'>
       <input type='checkbox' class='required-entry' name='groups[general][fields][terms][value]' value='1'>";
    return $html;
}

Adding a hidden input with the same name before the checkbox will ensure that something is sent via post even if you don't check the checkbox. When checking the checkbox, the hidden input value will be ignored.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top