2

I am trying to add a contacts form on multiple cms/products pages. I copied the default contacts form phtml and pasted it into a new phtml file. However, submitting the form redirects to the contacts page when I want it to remain on the page they completed the form on. So I created a custom module in order to override the controller and change the redirect.

Here are my files:

/app/etc/modules/SiDev_ModalContacts.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <SiDev_ModalContacts>
            <active>true</active>
            <codePool>local</codePool>
        </SiDev_ModalContacts>
    </modules>
</config>

/app/code/local/SiDev/ModalContacts/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <SiDev_ModalContacts>
            <version>0.1.0</version>
        </SiDev_ModalContacts>
    </modules>

    <frontend>
        <routers>
            <modalcontacts>
                <use>standard</use>
                <args>
                    <module>SiDev_ModalContacts</module>
                    <frontName>modalcontacts</frontName>
                </args>
            </modalcontacts>
        </routers>
    </frontend>
</config>

/app/code/local/SiDev/ModalContacts/controllers/IndexController.php:

<?php

require_once Mage::getModuleDir('controllers', 'Mage_Contacts').DS.'IndexController.php';

class SiDev_ModalContacts_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }

                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as quickly 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('*/*/');
        }
    }
}
?>

And for the form, I put:

<form id="contactForm" class="scaffold-form" action="<?php echo $this->getUrl('modalcontacts/index/post') ?>" method="post">

Upon submitting this form, I am sent to example.com/modalcontacts/index/post (not my real website), which is a 404 page, and no email is sent. I checked in the magento backend and it recognizes SiDev_ModalContacts as a module and it is enabled. And I have refreshed/flushed the magento cache from the backend. It seems to not recognize that my controller is there. As far as I'm aware, the correct method to set the form action to the controller is "frontName/controller/action" which would in fact be "modalcontacts/index/post", but it doesn't work. What am I missing?

Thank you for any help.

0

You need to add config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <SiDev_ModalContacts>
        <version>0.1.0</version>
    </SiDev_ModalContacts>
</modules>

<frontend>
    <routers>
        <modalcontacts>
            <use>standard</use>
            <args>
                <module>SiDev_ModalContacts</module>
                <frontName>modalcontacts</frontName>
            </args>
        </modalcontacts>
    </routers>
</frontend>
 <global>
    <rewrite>        
        <sidev_modalcontacts_contacts_indexcontroller>
            <from><![CDATA[#^/contacts/index/#]]></from> <!-- Mage_Contacts_IndexController  -->
            <to>/modalcontacts/contacts_index/</to> <!-- SiDev_ModalContacts_Contacts_IndexController  -->
        </sidev_modalcontacts_contacts_indexcontroller>
    </rewrite>
    </global>
</config> 

Your controller code as below:

app\code\local\SiDev\ModalContacts\controllers\Contacts\IndexController.php

<?php
require_once "Mage/Contacts/controllers/IndexController.php";  
class SiDev_ModalContacts_Contacts_IndexController extends Mage_Contacts_IndexController{
 //your code goes here
}
  • I added the additional code into config.xml, and made the changes to IndexController.php, but I am still led to a 404 page and no mail is sent, so it still is not calling the controller. With these changes, do I need to make any changes to the form action="modalcontacts/index/post" or does that stay the same? – joneill Jun 17 '16 at 16:50
0

I couldn't find any mistakes in your code. First thing that you need to check is, whether your controller is invoking or not. For this simply put this in your action method.

public function postAction()
{
    echo "I am here";
    die();
}

If you can see I am here while submitting your form, then this means your configurations are working and controller action works just fine. If that is the case, then your questions has relevance.

Why does email not send ?

Check email configuration. Check you are using correct template, check valid sender, receivers are there. Check subject is there. There are lot of reasons for not send an e-mail.

Why I am getting 404 page ?

This is because, through out your controller you are using below function for redirection.

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

This will preserve routename and controller and use index as action. Due to this it will redirect to modalcontacts/index/index in this context which is not defined in your controller and hence you will get a 404 page.

Instead of that, you need to use below function :

$this->_redirectReferer();

This function will redirect to referred page which is the original page from which you submitted your custom contact form


[This answer is a part of #mageStackDay5. It is an event conducted by Magento Community members as part of increasing the question-answer ratio. For more information http://www.magestackday.com/]

  • I replaced my postAction() function with yours, and the form still led directly to a 404 page. I also tried changing to redirectReferer() (and removing the die function) but still went to 404 page, and no mail was sent, so I am sure that the controller is not being invoked. Thank you for your help though, I will be sure to use redirectReferer() once I get the controller working. – joneill Jun 17 '16 at 16:48

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.