Magento – getFormAction() returns empty string

formsphp-5.4

I am embedding the contacts form onto a CMS page with this bit of layout XML:

<reference name="content">
<block type="core/template" name="cmsContactForm" form_action="/contacts/index/post" template="contacts/form.phtml"></block>
</reference>

However, when I am on that CMS page this bit of code on the contacts form page:

<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">

The getFormAction() method returns an empty string, resulting in the contact form failing as it just refreshes the page instead of submitting the information in the contact form.

Can anyone tell me how to get a proper form action from getFormAction()?

Is there a relevant helper that I can employ on a CMS page?

Best Answer

In Magento post action for contact page is set in controller class

Mage_Contacts_IndexController

In action indexAction()

$this->getLayout()->getBlock('contactForm')
            ->setFormAction( Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure())) );

If you declare the post action in block xml

<block type="core/template" name="cmsContactForm" form_action="/contacts/index/post" template="contacts/form.phtml"></block>

You can get it in block file which you are using as core/template and you can not get it in phtml directly.

You can use directly like

<form action="<?php echo Mage::getUrl('contacts/index/post', array('_secure' => $this->getRequest()->isSecure())); ?>" id="contactForm" method="post">
Related Topic