1

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.

2

1 Answer 1

4

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.

4
  • can you please help me in detail ?
    – Krupali
    Nov 7, 2016 at 9:46
  • Take a look at the form fields added without a frontend_model. Any field from the core. And you should see the way that the name attribute looks like. That structure is needed in order to read the values properly and store them in the database.
    – Marius
    Nov 7, 2016 at 9:55
  • thank you :) it works and is saving in database. but if i unchecked check box then there is no effect is come in database. instead of this it saving 1 every time (whether check box is checked or not. )
    – Krupali
    Nov 7, 2016 at 10:05
  • see my edit. Maybe it helps.
    – Marius
    Nov 7, 2016 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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