Displaying Global Messages in Magento 1.9 Admin Panel
In Magento 1.9, showing global messages in the admin panel requires a combination of front-end form handling and back-end message dispatching. Let’s walk through the process step-by-step.
Step 1: Creating the Front-End Form
First, you need to create an HTML form that accepts user input. This form will be submitted when the user clicks “submit”.
<form action="index.php" method="post">
<input type="text" name="mymsg" />
<input type="submit" name="submit" value="submit" />
</form>
This code creates a simple text input field for the message and a submit button. The form is posted to index.php using the POST method.
Step 2: Handling the Form Submission
The next step involves catching the form submission on the server side (Magento backend) and processing it accordingly.
<?php
if(isset($_POST["submit"]))
{
// Your code here
}
?>
Inside this PHP block, you need to validate the input and generate a message for display in the admin panel. Magento 1.9 provides a built-in functionality for displaying messages: Mage::getSingleton('core/session')->addNotice($message).
Step 3: Dispatching the Message
To show the message in the global messages area of the admin panel, you need to dispatch it using the Mage::getSingleton('core/session') object.
<?php
if(isset($_POST["submit"]))
{
$message = $_POST['mymsg'];
Mage::getSingleton('core/session')->addNotice($message);
}
?>
In this code, we first fetch the message from the POST data. Then, we use Mage::getSingleton('core/session')->addNotice() to add a notice-level global message.
Step 4: The Completed Solution
Putting it all together:
<form action="index.php" method="post">
<input type="text" name="mymsg" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_POST["submit"]))
{
$message = $_POST['mymsg'];
Mage::getSingleton('core/session')->addNotice($message);
}
?>
When the form is submitted, Magento captures the message and dispatches it as a notice-level global message in the admin panel.
Remember, this approach works for displaying messages once. If you need to display multiple messages or maintain persistent messages across sessions, consider using Mage::getSingleton('adminhtml/session')->addError($message) instead of addNotice.
Conclusion
In Magento 1.9, showing global messages in the admin panel involves a combination of front-end form handling and back-end session manipulation. By following this approach, you can easily display user-generated messages in the admin interface.
For more advanced messaging features or for working with newer versions of Magento, I recommend checking out our guide on displaying custom messages in your Magento application at Laravel Company.