cancel
Showing results for 
Search instead for 
Did you mean: 

Contact form in CMS page not working

Contact form in CMS page not working

Hello,

 

I've set up a CMS page for a contact form using the following code:

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

 

But the form just returns with 

 

Fatal error: Call to a member function setFormAction() on a non-object in /var/www/html/app/code/core/Mage/Contacts/controllers/IndexController.php on line 55

 

Can anyone suggest a fix please?

 

Thanks

 

kym

10 REPLIES 10

Re: Contact form in CMS page not working

After a post is submitted, this contact form redirects to "contacts/index". it looks like your contacts page (url "contacts/index") is missing a block with the name "contactForm". 

 

Make sure you have a layout xml file with <contacts_index_index>. By default, it's located in a file by the name of contacts.xml, try to find the file in app/design/[your_package]/[your_theme]/layout/contacts.xml


As a minimum it must contain:

 

<contacts_index_index translate="label">
        <reference name="content">
            <block type="core/template" name="contactForm" template="contacts/form.phtml"/>
        </reference>
    </contacts_index_index>

If this doesn't work, try pasting this code to another layout, for example to page.xml 


Aitoc is a group of distinguished web developers that have been building for Magento since 2009.
See our extensions for Magento 2.

Re: Contact form in CMS page not working

Hello and thank you for replying.

 

I've check the contacts.xml and it seems to have the code you mentioned. I've pasted it below... am I missing something? 

 

-->
<layout version="0.1.0">
<default>
<reference name="footer_links">
<action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled"><label>Contact Us</label><url>contacts</url><title>How To Contact Us/title><prepare>true</prepare></action>
</reference>
</default>

<contacts_index_index translate="label">
<label>Contact Us Form</label>
<reference name="head">
<action method="setTitle" translate="title" module="contacts"><title>Contact Form</title></action>
</reference>
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
<action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Form</title></action>
</reference>
<reference name="content">
<block type="core/template" name="contactForm" template="contacts/form.phtml"/>
<block type="cms/block" name="contact-text-add">
<action method="setBlockId"><block_id>contact-text</block_id></action>
</block>
</reference>
</contacts_index_index>
</layout>

Re: Contact form in CMS page not working

There's an error in the xml code. 

 In

<title>How To Contact Us/title>
you're missing an "<". 

 

It has to be to be:
<title>How To Contact Us</title>


This error ruins the entire xml file.


Here is a correctly formatted file for you:

 

<layout version="0.1.0">
    <default>
        <reference name="footer_links">
            <action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled">
                <label>Contact Us</label>
                <url>contacts</url>
                <title>How To Contact Us</title>
                <prepare>true</prepare>
            </action>
        </reference>
    </default>
    <contacts_index_index translate="label">
        <label>Contact Us Form</label>
        <reference name="head">
            <action method="setTitle" translate="title" module="contacts">
                <title>Contact Form</title>
            </action>
        </reference>
        <reference name="root">
            <action method="setTemplate">
                <template>page/2columns-right.phtml</template>
            </action>
            <action method="setHeaderTitle" translate="title" module="contacts">
                <title>Contact Form</title>
            </action>
        </reference>
        <reference name="content">
            <block type="core/template" name="contactForm" template="contacts/form.phtml"/>
            <block type="cms/block" name="contact-text-add">
                <action method="setBlockId">
                    <block_id>contact-text</block_id>
                </action>
            </block>
        </reference>
    </contacts_index_index>
</layout>

 


Aitoc is a group of distinguished web developers that have been building for Magento since 2009.
See our extensions for Magento 2.

Re: Contact form in CMS page not working

I'm an idiot! Shoudl of spotted that! Thank you, you're a super star!

Re: Contact form in CMS page not working

Hi again,

 

it's still workign alot better, but it's now returning this error. 

 

“Unable to submit your request. Please, try again later”

 

Any ideas? Smiley Very Happy

Re: Contact form in CMS page not working

Hi @TWSdesign

 

This can be many things, and unfurtuanlly this is one of the few places where Magento actually doesn't log the exceptions, properly to prevent saving posted data &colon;-).

 

Check around iline 117, in the catched exception in the file:

 

app/code/core/Mage/Contacts/controllers/IndexController.php

 

This hopefully this will help you :-)

 

Re: Contact form in CMS page not working

Hi Theis,

 

Thank you for your reply.

 

I've had a look at the line in that php file, but to be honest I'm not 100% of what I'm looking for!

 

I've pasted it below. Please let me knwo if you stop anything.

 

 

THANK YOU Smiley Happy

 

class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
{

const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
const XML_PATH_ENABLED = 'contacts/contacts/enabled';

public function preDispatch()
{
parent:Smiley TonguereDispatch();

if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
$this->norouteAction();
}
}

public function indexAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('contactForm')
->setFormAction( Mage::getUrl('*/*/post') );

$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
$this->renderLayout();
}

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 soon 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('*/*/');
}
}

}

Re: Contact form in CMS page not working

Hi @TWSdesign

 

The easiest way for you to debug this without any tools for it would be to add a little piece of code the line:

 

Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));

 

The code should be as follows:

 

echo '<pre>';
echo $e;
echo '</pre>';
exit;

This will print the exact location where the exception happens. Let me know what you find :-)

 

 

 

Re: Contact form in CMS page not working

I also have an issue with the contact form,when I submit the form its show a success message but emails not arrives. I have tested the email addresses they are setup correctly but still no luck to receive the emails:

 

    HERE is the code from my CMS page:

    {{block type="core/template" name="contactForm" form_action="http://quickpro.nextmp.net/index.php/contacts/index/post/" template="contacts/form.phtml"}}