Reputation: 5288
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('*/*/');
Upvotes: 8
Views: 40019
Reputation: 1019
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:
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!
Upvotes: 0
Reputation: 1483
From my experience, I suppose change the success message after from submitting the contacts page.
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'}}
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>
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());
Upvotes: 1
Reputation: 31
Could also just create custom url redirect.
Upvotes: 3
Reputation: 382
For the next person
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!
Upvotes: 5
Reputation: 2965
The combined solution is given in both answers from @Simon and the others
$this->_redirect('*/*/')
everywhere in app/code/core/Mage/Contacts/controllers/IndexController.php
to $this->_redirectReferer();
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_viewBest to copy the files to 'local'
Upvotes: -1
Reputation: 602
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.
Upvotes: 2
Reputation: 4179
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
Upvotes: 14
Reputation: 5288
IndexController.php under app > code > core > Mage > Contacts > controllers
changed
$this->_redirect('*/*/');
to
$this->_redirect('');
and it re-directs to the homepage now.
Upvotes: 1