Search code examples
magentomagento-1.9

Magento contact form on CMS page shows 404 error


I added my contact form.phtml to a cms page with the following code:

{{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}}

However when I submit the contact form it will show a 404 error because the URL is being routed to www.domain.de/contacts/index/post, which doesn't exist because I use URL rewrites with store codes (de/en).

So the routing should go to www.domain.de/de/contacts/index/post

This only works when I alter the code as follows:

{{block type="core/template" name="contactForm" form_action="http://domain.de/de/contacts/index/post" template="contacts/form.phtml"}}

Since I am pretty new to magento I didn't find a solution and I dont know if this could be a problem with the .htaccess file.

Did anyone have the same issue and knows how to fix it?

Update:

Just to make sure if I did it correctly: I created \app\code\local\MARG\ContactForm\etc\config.xml\ with the following content:

<config>
    <modules>
        <MARG_ContactForm>
            <version>0.0.1</version>
        </MARG_ContactForm>
    </modules>
    <global>
        <blocks>
            <ContactForm>
                <class>MARG_ContactForm_Block</class>
            </ContactForm>
        <blocks>
    </global>
</config>

Then I created app\code\local\MARG\ContactForm\Block\ContactForm.php with content:

class MARG_ContactForm_Block_ContactForm extends Mage_Core_Block_Template
{
   protected function_construct()
   {
       $this ->setTemplate('contacts/form.phtml');
       parent::_construct();
   }

    public function getFormAction()
    {
        return Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure()));
    }
}

In app\etc\modules\MARG_ContactForm.xmlI put content:

<config>
    <modules>
        <MARG_ContactForm>
            <active>true</active>
            <codePool>local</codePool>
        </MARG_ContactForm>
    </modules>
</config>

In the CMS page I added {{block type="MARG/ContactForm" name="contactForm"}}

Is there something i overlooked?

Edit:

i have done it exactly as described and added {{block type="ContactForm/ContactForm" name="contactForm"}} to the CMS page. However it still shows a blank page the exception.log says

exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_ContactForm_Block_ContactForm' in C:\dev\plain\magento\app\Mage.php:595 Stack trace: 
#0 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(495): Mage::throwException('Invalid block t...') 
#1 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('ContactForm/Con...', Array)

Solution

  • The problem here is that the block is type Mage_Core_Block_Template which doesn't have a function to turn form action into a fully qualified URL. Normally that happens in Mage_Contacts_IndexController but of course your CMS page is not using that controller.

    The solution is to make a block type which suits your purpose. I'll assume you know how to make a module and that it's name is "example".

    class Example_Example_Block_Contacts extends Mage_Core_Block_Template
    {
        protected function _construct()
        {
            $this->setTemplate('contacts/form.phtml');
            parent::_construct();
        }
    
        public function getFormAction()
        {
            // copied from Mage_Contacts_IndexController::indexAction()
            return Mage::getUrl('contacts/index/post', array('_secure' => $this->getRequest()->isSecure()));
        }
    }
    

    This makes your CMS page simpler too. It only needs to include:

    {{block type="example/contacts" name="contactForm"}}