5

How display success a message after submit the form. This is my site a link

  • Going to need a lot more information than that to give an answer that is actually useful. – Jonathan Hussey Nov 14 '14 at 11:00
15

In php this type of work implement on session variable.

Magento is doing by

set success message using ->addSuccess('YOUR MSG');.

and set error message using ->addError('YOUR ERROR MSG')

Those magento functions are Object of

  1. Mage::getSingleton('core/session')

  2. Mage::getSingleton('customer/session')

  3. Mage::getSingleton('catalog/session')

So you can set this message.

  1. Mage::getSingleton('core/session')->addSuccess('YOUR MSG');
  2. Mage::getSingleton('customer/session')->addSuccess('YOUR MSG');

etc

This message is shown at phtml file using add below code:

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>

Most Important Note:

This session message is one available up to one render.

ON Message display page ,you need to initialized this session using code initLayoutMessages() and you need add this code before $this->renderLayout(); function i.e Start of render layout

Just like:

  $this->_initLayoutMessages('customer/session');
  $this->_initLayoutMessages('catalog/session');

Suppose you have set message at Mage::getSingleton('customer/session') and on message display page you have not initialized $this->_initLayoutMessages('customer/session');

Then you did not get this message.

A Good example is contact page:

public function postAction()
{
   ........
    if ( $post ) {
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
       .............
            Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
            $this->_redirect('*/*/');

            return;
        } catch (Exception $e) {
            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }

    } else {
        $this->_redirect('*/*/');
    }
}

Message is show at indexAction:

  public function indexAction()
    {
        $this->loadLayout();
        $this->getLayout()->getBlock('contactForm')
            ->setFormAction( Mage::getUrl('*/*/post') );

        $this->_initLayoutMessages('customer/session');
        $this->_initLayoutMessages('catalog/session');
        $this->renderLayout();
    }
  • 1
    Having to initialize the messages put me on the track ! – hellimac Jul 29 '15 at 10:41
  • Thx, I have the problem that on product view there is no error message shown. So the reason is because that message is already shown in customer/account/login i guess... So I had to solve it at another way. I could also just redirect the user to the login page but I wanted him to stay on the product. I tried to add $this->_initLayoutMessages('customer/session'); and $this->_initLayoutMessages('catalog/session'); to the _prepareLayout function of the class Mage_Catalog_Block_Product_View in app\code\core\Mage\Catalog\Block\Product\View.php but then the site stopped working. – Black Jan 24 at 15:53
5
  1. Set Your Message.
Mage::getSingleton("core/session")->addSuccess("Add success message"); 
Mage::getSingleton("core/session")->addError("Please login");
Mage::getSingleton("core/session")->addNotice("Add notification message");
  1. Display Your Message Whereever You Want.(Optional If use custom Extension Or not Defined)
<?php echo $this->getChildHtml('global_messages'); ?>

3.Define Block in Layout. (Optional If use custom Extension Or not Defined)

<block type="core/messages" name="global_messages" as="global_messages"/>

1

To display a success message use Mage::getSingleton('customer/session')->addSuccess()

Take a look at /app/code/core/Mage/Contacts/controllers/IndexController.php

    $post = $this->getRequest()->getPost();
    if ( $post ) {
        try {
            $postObject = new Varien_Object();
            $postObject->setData($post);

            //save to db or email

            Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }
    } else {
        $this->_redirect('*/*/');
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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