2

The button in my module:

system.xml

                <update translate="label">
                    <frontend_type>button</frontend_type>
                    <frontend_model>mateusvitali_sigepweb/button</frontend_model>
                    <sort_order>300</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                </update>   

I want to call a method (php) by clicking on the button. I created the class MateusVitali_SigepWeb_Block_Button too.


Ok, I tried to follow this tutorial: http://www.atwix.com/magento/add-button-to-system-configuration/

But the method is not called and the alert does not appear.

app/design/adminhtml/default/default/template/MateusVitali/system/config/updateButtin.phtml

<script type="text/javascript">
    //<![CDATA[
    function update() {
        new Ajax.Request('<?php echo $this->getAjaxCheckUrl() ?>', {
            method:     'get',
            onSuccess: function(transport){
                if (transport.responseText){
                    alert('Updated!');
                }
            }
        });
    }
    //]]>
</script>

<?php echo $this->getButtonHtml() ?>

app/code/local/MateusVitali/SigepWeb/Controllers/Adminhtml/MateusVitaliSigepWebController.php

class MateusVitali_SigepWeb_Adminhtml_MateusVitaliSigepWebController extends Mage_Adminhtml_Controller_Action
{
    /**
     * Return some checking result
     *
     * @return void
     */
    public function updateAction()
    {
        $result = 1;
        Mage::app()->getResponse()->setBody($result);
    }
}

app/code/local/MateusVitali/SigepWeb/Block/Button.php

class MateusVitali_SigepWeb_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    /*
     * Set template
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('mateusvitali/system/config/updateButton.phtml');
    }

    /**
     * Return element html
     *
     * @param  Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        return $this->_toHtml();
    }

    /**
     * Return ajax url for button
     *
     * @return string
     */
    public function getAjaxCheckUrl()
    {
        return Mage::helper('adminhtml')->getUrl('adminhtml/adminhtml_mateusvitalisigepweb/update');
    }

    /**
     * Generate button html
     *
     * @return string
     */
    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(array(
            'id'        => 'update_button',
            'label'     => 'Atualizar Serviços',
            'onclick'   => 'javascript:update(); return false;'
        ));

        return $button->toHtml();
    }
}

1 Answer 1

0

Since you probably don't want to reload the page, you'll need to create a custom controller and call it with AJAX. Use the prototype.js AJAX methods in your custom button: http://api.prototypejs.org/ajax/Ajax/Request/

1
  • Ok, added information in the initial doubts Commented Jan 1, 2016 at 20:41

Your Answer

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

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