0

I have made a module with a contact form. The Submit button calls a controller which get $ post and send emails both to client and admin. I would like after form submission to redirect to the form page with a success or error message display (as works the standard magento contact form)

It is almost working but I have something not clear in the process…

  1. To display my form i have created a CMS page with {{block type="dptrentejours/dptrentejoursformblock" name="dptrentejours_dptrentejoursformblock" template="dptrentejours/dptrentejoursform.phtml"}}

Is it the good way, or may i rather call www.myshop.com/dptrentejours/index ?

  1. the indexAction does not load the form. I have got an empty page core.

after submission if I go back to the url of the CMS page, the success / error message is displayed

My controller :

 <?php
    class Mine_Dptrentejours_IndexController extends Mage_Core_Controller_Front_Action
    {
       public function indexAction()
       {
            $this->loadLayout();
            $this->renderLayout();
       }

       public function sendrequestAction()
       {
        if ($post = $this->getRequest()->getPost()) 
            {   
                $postObject = new Varien_Object();
                $postObject->setData($post);
... 

        try
        {
            $mail2->send();
            try
            {
                $mail->send();
                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 $error)
            {
                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }                   
        }               
        catch(Exception $error)
        {
            Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
            $this->_redirect('*/*/');
            return;
        }
            }
       }
    }

My Layout.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
        </reference>
        <reference name="head">
            <action method="addItem"><type>skin_css</type><name>css/mine_dptrentejours/dptrentejours_form.css</name></action>
        </reference>
    </default>

    <routeurfrontend_index_index>
        <reference name="content">
            <block type="dptrentejours/dptrentejoursformblock"  name="dptrentejours_dptrentejoursformblock"
                          template="dptrentejours/dptrentejoursform.phtml" />
        </reference>
    </routeurfrontend_index_index>

</layout>

config.xml :

<?xml version="1.0"?>
<config>
    <modules>  
        <Mine_Dptrentejours>  
            <version>0.0.1</version>  
        </Mine_Dptrentejours>  
    </modules>

    <frontend>
        <routers>
            <dptrentejours>
                <use>standard</use>
                <args>
                    <module>Mine_Dptrentejours</module>
                    <frontName>dptrentejours</frontName>
                </args>
            </dptrentejours>
        </routers>    
        <layout>
            <updates>
                <dptrentejours>
                    <file>dptrentejours.xml</file>
                </dptrentejours>
            </updates>
        </layout>  
         <translate>
            <modules>
                <dptrentejours>
                    <files>
                        <default>Mine_Dptrentejours.csv</default>
                    </files>
                </dptrentejours>
            </modules>
        </translate>
    </frontend>


    <adminhtml>
    </adminhtml>

    <global>
        <blocks>
            <dptrentejours>
                <class>Mine_Dptrentejours_Block</class>
            </dptrentejours>
        </blocks>
        <models>
            <dptrentejours>
                <class>Mine_Dptrentejours_Model</class>
            </dptrentejours>
        </models>
        <helpers>
            <dptrentejours>
                <class>Mine_Dptrentejours_Helper</class>
            </dptrentejours>
        </helpers>        
    </global>

</config>

dptrentejoursform.phtml

<?php
/** Formulaire de demande de paiement 30j */
?>
<div class="page-title">
    <h1 style="color: #636363;"><?php echo $this->__('30 days Payment request') ?></h1>
</div>
<form action="<?php echo Mage::getBaseUrl().'dptrentejours/index/sendrequest' ?>" id="DP30Form" method="post">
    <div class="fieldset">
        <p class="required" style="color: #FF0000;"><?php echo $this->__('* Required Fields') ?></p>
        <h2 class="legend" style="color: #636363; margin-left: 2em;"><?php echo $this->__('Contact Information') ?></h2>
        <ul class="form-list" style="margin-left: 4em;">
                <div class="field">
                    <label for="firstname" class="required"><em>*</em><?php echo $this->__('Firstname') ?></label>
                    <div class="input-box">
                        <input name="firstname" id="firstname" title="<?php echo $this->__('Firstname') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="lastname" class="required"><em>*</em><?php echo $this->__('Lastname') ?></label>
                    <div class="input-box">
                        <input name="lastname" id="lastname" title="<?php echo $this->__('Lastname') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="userid" class="required"><em>*</em><?php echo $this->__('Account Username') ?></label>
                    <div class="input-box">
                        <input name="userid" id="userid" title="<?php echo $this->__('Account Username') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="email" class="required"><em>*</em><?php echo $this->__('Email') ?></label>
                    <div class="input-box">
                        <input name="email" id="email" title="<?php echo $this->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
                    <div class="input-box">
                        <input name="telephone" id="telephone" title="<?php echo $this->__('Telephone') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
        </ul>
        <h2 class="legend" style="color: #636363; margin-left: 2em;"><?php echo $this->__('Company Information') ?></h2>
        <ul class="form-list" style="margin-left: 4em;">
                <div class="field">
                    <label for="companyname" class="required"><em>*</em><?php echo $this->__('Company Name') ?></label>
                    <div class="input-box">
                        <input name="companyname" id="companyname" title="<?php echo $this->__('Company Name') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="siret" class="required"><em>*</em><?php echo $this->__('Company immatriculation number') ?></label>
                    <div class="input-box">
                        <input name="siret" id="siret" title="<?php echo $this->__('Company immatriculation number') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="adresse1" class="required"><em>*</em><?php echo $this->__('Address Line 1') ?></label>
                    <div class="input-box">
                        <input name="adresse1" id="adresse1" title="<?php echo $this->__('Address Line 1') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <label for="adresse2"><?php echo $this->__('Address Line 2') ?></label>
                    <div class="input-box">
                        <input name="adresse2" id="adresse2" title="<?php echo $this->__('Address Line 2') ?>" value="" class="input-text" type="text" />
                    </div>
                </div>                
                 <div class="field">
                    <label for="postalcode" class="required"><em>*</em><?php echo $this->__('Postal Code') ?></label>
                    <div class="input-box">
                        <input name="postalcode" id="postalcode" title="<?php echo $this->__('Postal Code') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>      
                <div class="field">
                    <label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
                    <div class="input-box">
                        <input name="city" id="city" title="<?php echo $this->__('City') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>    
                <div class="field">
                    <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                    <div class="input-box">
                        <input name="country" id="country" title="<?php echo $this->__('Country') ?>" value="" class="input-text required-entry" type="text" />
                    </div>
                </div>    
        </ul>
        <h2 class="legend" style="color: #636363; margin-left: 2em;"></h2>
        <ul class="form-list" style="margin-left: 4em;">  
                <div class="field">
                    <div class="input-box">
                        <input name="TermsandCondition" id="TermsandCondition" title="<?php echo $this->__('Terms and Conditions') ?>" value="1" class="checkbox required-entry" type="checkbox" />
                        <label for="TermsandCondition" class="required"><em>*</em></label>
                        <label for="TermsandCondition"><?php echo $this->__('I have read and I accept the') ?> <a href="<?php echo Mage::getBaseUrl(); ?>/paiement_30j_Terms"><?php echo $this->__('30 days payment Terms and Conditions') ?></a></label>
                    </div>
                </div>           
        </ul>        
    </div>
    <div class="buttons-set" style="max-width: 600px;">
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
    </div>
</form>

<script type="text/javascript">
    //< ![CDATA[
        var customForm = new VarienForm('DP30Form');
    //]]>
</script>

thank you for your help,

  • show your modules config.xml file and dptrentejoursform.phtml – Rajeev K Tomy Nov 3 '14 at 11:22
  • I edited the post – Alexglvr Nov 3 '14 at 13:20
1

Take care of the following points.

1) Your router unique alias name is dptrentejours (as per your config.xml). So in your layout file you need to use dptrentejours_index_index as layout handle instead of routeurfrontend_index_index. This is why form is not obtained in frontend.

2) Inside the custom layout handle, you have included your block in the content section. You are right on that point. The custom block type specified there is dptrentejours/dptrentejoursformblock. This means Magento require this block to be really be defined inside your module. Other wise you will not get your form in frontend. So make sure this block is defined like this.

File : app\code\local\Mine/Dptrentejours/Block/Dptrentejoursformblock.php

<?php
class Mine_Dptrentejours_Block_Dptrentejoursformblock 
    extends Mage_Core_Block_Template {
}

3) Make sure your template form exist in the right location. As per your layout xml file, it should be held at

app\design\frontend\<package>\<theme>\template\dptrentejours/dptrentejoursform.phtml.

Since you are adding your custom block directly via layout file, you dont need to use any cms page to show the form.

Side Note : Always remember to clear the cache

  • Thank you. I now have the dptrentejours/index working. But, after submitting the form, the redirection works but without the success / error message. I added the redirect / message part of the controller in the post... – Alexglvr Nov 4 '14 at 9:38
  • put this <?php echo $this->getMessagesBlock()->getGroupedHtml() ?> inside your phtml file at top. It may resolve the issue. If my answer helped you, dont forget to accept the answer – Rajeev K Tomy Nov 4 '14 at 9:42
  • I added the php code, but still the same. If I load the form using dptrentejours/index, no message. But if I go back on the CMS page, the message are displayed. If I use the CMS page for the form, I am redirected to dptrentejours/index, with no message... – Alexglvr Nov 4 '14 at 9:50
  • Not sure what is the problem again. As per my knowledge it should work by now – Rajeev K Tomy Nov 4 '14 at 10:21
  • with Mage::getSingleton('core/session')->addSuccess($this->__('Your inquiry ...')); it works ! Thanks again ! – Alexglvr Nov 4 '14 at 11:37

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.