8
votes

how can i re-direct to a particular page after the user submits the contact form in Magento? form.phtml has

<form action="<?php echo Mage::getUrl(); ?>contacts/index/post/" id="contactForm" method="post">

but i'm not sure where to find the php file that controls the email sending and redirects. any ideas? thanks

EDIT: found this in IndexController.php under app > code > core > Mage > Contacts > controllers

$this->_redirect('*/*/');
8

8 Answers

14
votes

I know its answered, just sharing my experience. I had made a contact form through a CMS page. The form worked fine. But after submitting it, it would redirect to the magento contact form. To redirect it back to CMS page, i had to put

$this->_redirect('contactus');

where contactus is the URL identifier.

Also after redirect, the success / error message would not show up. For that i had to make changes here.

Go to /app/design/frontend/default/yourstore/template/contacts/form.phtml

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

with:

<?php Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?> 

I got the solution from here

5
votes

For the next person

  1. go to silk software and create a new module using their module creator http://www.silksoftware.com/magento-module-creator/
  2. Enter your company name and module name
  3. Then change "Need Rewrite Magento Class" to Yes
  4. click "Add Class" the class name will be Mage_Contacts_IndexController
  5. This will create a module with everything you need
  6. Add the postAction method from the Core Controller to your newly created controller
  7. Then change the redirect to redirectReferer() at the end of the postAction method

The module creator will create everything you need to overload the Contacts Controller and save you time from troubleshooting typos. Also, save yourself the trouble down the road of editing the core files directly.

DO NOT EDIT THE CORE FILES!

3
votes

Could also just create custom url redirect.

  • id path - contacts/index
  • request path - contacts/index
  • target path - 'redirect url'
2
votes

To avoid overwriting core files and harm update compatibility, I overloaded the controller as described here: Tutorial: Overload a controller

<frontend>
    <routers>
        <contacts>
            <args>
                <modules>
                    <My_Module_Contacts before="Mage_Contacts">My_Module_Contacts</My_Module_Contacts>
                </modules>
            </args>
        </contacts>
    </routers>
</frontend>

And rewrote $this->_redirect('*/*/') to $this->_redirectReferer('contacts/index'), so you get redirected to the previous page, and if no referer was set, to /contacts/index as a fallback.

Also I changed form.phtml from

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

to

<div id="messages_product_view">
  <?php Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
  <?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>
</div>

to display the error messages.

1
votes

IndexController.php under app > code > core > Mage > Contacts > controllers

changed

$this->_redirect('*/*/');

to

$this->_redirect('');

and it re-directs to the homepage now.

1
votes

From my experience, I suppose change the success message after from submitting the contacts page.

  1. I used the below code in my static page

    {{block type='core/template' name='contactForm' form_action='mywebsiteurl/contacts/index/post' template='contacts/form.phtml'}}
    
  2. I override the default controller action in my custom module config.xml file.

            <routers>
                <airhotels>
                    <args>
                        <modules>
                            <apptha_airhotels before="Mage_Contacts">Apptha_Airhotels</apptha_airhotels>
                        </modules>
                    </args>
                </airhotels>
            </routers>
    
  3. And in my custom controller(Apptha_Airhotels_ContactsController) action (postAction ) I used my custom message and redirect to custom url.

    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($formUrl); 
    

This form url is defined as : $formUrl = basename($this->_getRefererUrl());

0
votes

A pretty dirty but easy approach is to redirect /contact/index to /contact after form submission.

Say you extend the Magento Contact in your template like here:

enter image description here

Add file Magento_Contact/templates/form-submitted.phtml with the following content:

<?php
header('Location: /contact');
exit;

Then change Magento_Contact/layout/contact_index_index.html to use that template as below:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Contact</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form-submitted.phtml">
                <container name="form.additional.info" label="Form Additional Info"/>
            </block>
        </referenceContainer>
    </body>
</page>

Everything works, including the flash message.

Voila!

-1
votes

The combined solution is given in both answers from @Simon and the others

  • change $this->_redirect('*/*/') everywhere in app/code/core/Mage/Contacts/controllers/IndexController.php to $this->_redirectReferer();
  • and update the form phtml to include the error/success message by editing app/design/frontend/base/default/template/contacts/form.phtml and adding the line <?php Mage::app()->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?> in messages_product_view

Best to copy the files to 'local'