Create a form in magento admin panel

Create a form in magento admin panel

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create a form in magento admin panel

Creating a Form in Magento Admin Panel: A Comprehensive Guide

Creating a custom form in the Magento admin panel can be a complex task for developers. In this post, we’ll guide you through the process using an example of creating a form within your own module.

Understanding the Problem

The user has faced several issues while trying to create a form in their custom Magento module:

  1. The form doesn’t appear when clicking on the menu item.
  2. There are no errors returned by the system, only a warning about simplexml_load_string().
  3. Moving the menu configuration from config.xml to adminhtml.xml results in the menu disappearing.

Analyzing the Current Setup

Let’s break down the current setup provided:

The Configuration XML

In config.xml, the user has correctly configured the admin routes and menu for their module ‘Mynamespace_Skipcart’. However, there seems to be an issue with how the layout files are referenced in this configuration.

<admin>
    <routers>
        <test>
            <use>admin</use>
            <args>
                <module>Mynamespace_Skipcart</module>
                <frontName>Skipcart</frontName>
            </args>
        </test>
    </routers>
</admin>
<adminhtml>
    <menu>
        <tutorial_menu translate="title" module="skipcart">
            <title>Skip Cart</title>
            <sort_order>9999</sort_order>
            <children>
                <first_page module="skipcart">
                    <title>Our First Page</title>
                    <action>Skipcart/Adminhtml_index/index</action>
                </first_page>
            </children>
        </tutorial_menu>
    </menu>
    <layout>
        <updates>
            <skipcart>
                <file>Skipcart.xml</file>
            </skipcart>
        </updates>
    </layout>
</adminhtml>

The Layout XML

In app/design/frontend/default/default/layout/skipcart.xml, the user has attempted to create a layout update for their module. However, there are some structural issues and missing elements that could be causing problems.

<?xml version="1.0"?>
<layout>
    <skipcart_adminhtml_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
        </reference>

         <update handle="skipcart_index_index"/>
        <reference name="content">
            <block type="adminhtml/template" name="skipcart" template="skipcart/add.phtml"/>
        </reference>
    </skipcart_adminhtml_index_index>
<!-- I miss the </layout> because I want to check if magento read this file.-->
</layout>

The Controller and Block

The user has created a controller Adminhtml/IndexController.php and a block referenced in their layout XML. However, the controller is currently just loading a layout and doesn’t seem to be processing any form data.

Solving the Issues

To resolve these issues, let’s address them one by one:

Fixing the Layout Structure

The main issue with skipcart.xml seems to be the missing closing </layout> tag. Additionally, you need to specify a handle for your layout update. The correct structure should look like this:

<?xml version="1.0"?>
<layout>
    <skipcart_adminhtml_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
        </reference>

         <update handle="skipcart_adminhtml_index_index"/>
        <reference name="content">
            <block type="adminhtml/template" name="skipcart" template="skipcart/add.phtml"/>
        </reference>
    </skipcart_adminhtml_index_index>
</layout>

Creating the Form Block

You need to create a new file add.phtml in your module’s view directory (usually app/design/adminhtml/default/default/template/skipcart/). Inside this file, you can define your form using standard HTML elements.

Here’s an example of what the add.phtml file could look like:

<form id="skipcart_form" action="<?php echo $this->getUrl('*/*/post') ?>" method="post">
    <table>
        <tr>
            <td><label for="name">Name:</label></td>
            <td><input type="text" name="name" id="name"></td>
        </tr>
        <!-- Add more form fields as needed -->
        <tr>
            <td colspan="2">
                <button type="submit" class="scalable button"><span><span>Save</span></span></button>
            </td>
        </tr>
    </table>
    <?php echo $this->getBlockHtml('form_key') ?>
</form>

Processing the Form Data

In your Adminhtml/IndexController.php file, you need to handle the form submission in a separate action method called postAction(). This is where you would process the form data and save it as needed.

Here’s an example of how you can modify your controller:

public function postAction()
{
    $post = $this->getRequest()->getPost();
    try {
        if (empty($post)) {
            Mage::throwException($this->__('Invalid form data.'));
        }

        // Process the form data here...
        // For example, save it to a database or perform some other operation

        $message = $this->__('Your form has been submitted successfully.');
        Mage::getSingleton('adminhtml/session')->addSuccess($message);
    } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
    }
    $this->_redirect('*/*');
}

Updating the Layout Handle

Finally, you need to update the handle in your config.xml file to match the layout XML:

<layout>
    <updates>
        <skipcart>
            <file>Skipcart.xml</file>
        </skipcart>
    </updates>
</layout>

Conclusion

Creating a custom form in the Magento admin panel requires careful configuration of your module’s XML files and proper handling of form data in your controller. By following the guidelines outlined above, you should be able to create a functional form that appears in the admin menu.

Remember to test your code thoroughly and check for any errors or warnings that may appear during the development process. If you’re still having issues, feel free to reach out to our team at Laravel Company for further assistance with your Magento development needs.

Happy coding!

Checkout Extensions Themes Admin

Our network