Magento : add custom button to an admin form

Magento : add custom button to an admin form

July 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : add custom button to an admin form

Magento: Adding Custom Buttons to Admin Forms

As an experienced developer, I frequently encounter cases where clients need to extend the functionality of their Magento admin panel by adding custom buttons to existing forms. This allows for more dynamic content management within the backend interface.

Understanding the Context

The user has created a simple form in an adminhtml module using the Mage_Adminhtml_Block_Widget_Form class. They aim to add a custom button labeled “Add Option” that, when clicked, appends additional input fields (textboxes or dropdowns) to the form.

The Challenge

The user has attempted to find a solution on StackOverflow and searched through the Magento core for an existing implementation they could extend. Although a relevant example exists in the catalog attribute management section, it’s not clear where the actual functionality resides due to the complexity of the inheritance chain.

To achieve this goal, I would suggest creating a custom button that triggers a JavaScript event to dynamically add input fields to the form. Here is a step-by-step guide:

Step 1: Add the Custom Button in the Form

First, you need to add the “Add Option” button to your existing form. You can do this by adding the following code inside the _prepareForm method of your Namespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form class:

$button = $this->getLayout()->createBlock('adminhtml/widget_button')
    ->setData(array(
        'label'     => Mage::helper('modulename')->__('Add Option'),
        'onclick'   => "addOptionToForm()",
        'class' => 'action-button',
    ));

$fieldset->setResetLabel(Mage::helper('modulename')->__('Back'));
$fieldset->setClass('fieldset-wide');
$fieldset->append($button);

Step 2: Create the JavaScript Function

Next, you need to create a JavaScript function that will handle the addition of new input fields. You can place this function inside your module’s JavaScript file (usually located in app/code/local/Namespace/Modulename/js/modulename.js).

function addOptionToForm() {
    var form = document.getElementById('modulename_form');
    var fieldset = document.createElement('fieldset');
    var inputField = document.createElement('input');
    inputField.setAttribute('type', 'text');
    inputField.setAttribute('name', 'new_option[]');
    fieldset.appendChild(inputField);
    form.appendChild(fieldset);
}

Step 3: Update the Form Processing Logic

When you submit the form, ensure that your custom fields (in this case, new_option[]) are properly processed on the server side. You can handle this in the controller action responsible for saving the data.

Conclusion and Best Practices

Adding a custom button to an admin form in Magento requires a combination of PHP and JavaScript code. By following these steps and utilizing the built-in functionality provided by Magento, you can create dynamic forms that allow users to add additional fields as needed.

Remember to keep your code organized and follow best practices such as using helper functions for translations and avoiding hardcoding values where possible. If you need further assistance or want to learn more about extending Magento’s admin panel, feel free to explore the Magento documentation or reach out to our team of experts at Laravel Company.

Happy coding!

Catalog Extensions Themes Admin

Our network