sergio
sergio

Reputation: 5260

Magento redirect to the page from that came

I have a module which had instaled a CMS page with some default URL and some text with form. On this form I have fields and submit. I validate my fields using ajax(send POST in controller of my module). If validation is OK, I redirect to the same page with "succes message".

The problem is, that the default URL of this CMS page in which I make redirect after succes submit can be changed in BO - that's why I cannot just $this->_redirect('default_URL') in my controller, because this url can be changed.

What should I do?

EDIT : Solution: use $this->_redirectReferer() in my controller after success validation

Upvotes: 0

Views: 10439

Answers (3)

dgianco
dgianco

Reputation: 1

Use this code for redirect to referer

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>

For example in custom login form:

<form method="post" action="<?php echo Mage::helper('customer')->getLoginPostUrl() ?>">
    <?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
...
...

Upvotes: 0

Sergey Kolodyazhnyy
Sergey Kolodyazhnyy

Reputation: 691

Try to put "back URL" to your form as a hidden field or you can use $this->_redirectReferer() to redirect back to CMS page.

See http://docs.magentocommerce.com/Mage_Core/Mage_Core_Controller_Varien_Action.html#method_redirectReferer

If you want to redirect to some certain CMS page (not the one where form is placed) you can do this:

  1. Add dropdown to system configuration to be able to select "Success page" (much better than hardcode cms page ID)

  2. Redirect to this page in your controller

Code:

$pageId = Mage::getStoreConfig('mymodule/config/success_page');
$page = Mage::getModel('cms/page')->load($pageId);
$this->_redirect($page->getUrlKey());

Upvotes: 3

Tobias
Tobias

Reputation: 1682

You can get a store config by using the getStoreConfig method, something like

Mage::getStoreConfig('helloworld_options/messages/hello_message');

See Alan's blog, about detailed instructions how to create custom config values (which you might already have) and to access them.

Upvotes: 0

Related Questions