Can anyone provide a dummy guide \ code snippets on how to create a front end form in Magento that posts data to a controller action.
Im trying to write a variant of the contact us from. (I know its easy to modify the contact us form, as outlined here). I'm trying to also create a feedback form with additional fields.
Given this basic form:
<form action="<?php echo $this->getFormAction(); ?>" id="feedbackForm" method="post">
<div class="input-box">
<label for="name"><?php echo Mage::helper('contacts')->__('Name') ?> <span class="required">*</span></label><br />
<input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="required-entry input-text" type="text" />
</div>
<div class="button-set">
<p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
<button class="form-button" type="submit"><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></button>
</div>
</form>
What are the basic step I need to take to get inputted name to a controller action for processing?
If any one is interested, I solved this by building my own module which was heavily based on the
Magento_Contacts
module.Here are some links that helped me figure things out.
http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table
http://inchoo.net/ecommerce/magento/magento-custom-emails/
To make
$this->getFormAction()
return the URL to your custom controller, you have two options:setFormAction()
somewhere else on the block.getFormAction()
.(1) is what happens in
Mage_Contacts_IndexController::indexAction()
, but (2) is the cleaner approach and I'm going to explain it in detail:Create a custom module
app/etc/modules/Stack_Form.xml
:app/code/local/Stack/Form/etc/config.xml
:This configuration registers the
stack_form
block alias for own blocks and thefeedback
front name for own controllers.Create custom block
app/code/local/Stack/Form/Block/Form.php
Here we implemented
getFormAction()
to generate the URL for our custom controller (the result will be BASE_URL/feedback/index/post).Create custom controller
app/code/local/Stack/Form/controllers/IndexController.php
If the form should behave exactly like the contact form, just with a different email template and additional form fields, there are two solutions that I have outlined at https://magento.stackexchange.com/q/79602/243 where only one of them actually requires a custom controller action to send the form:
How to use this custom block
You can add the form anywhere in the CMS using this code (CMS directive):
If you do this, you need to add "stack_form/form" to the block whitelist under System > Permissions > Blocks!
Or in the layout using this code (layout XML):
Solution without custom module
If you use the solution without custom controller and a single email template mentioned above, you can set the form action using layout XML as well.
To achieve this, we use the feature to call helpers as parameters for block actions. Unfortunately, the core helper does not have a public method to get a URL but the helper from
Mage_XmlConnect
has, so you can use that one:In the CMS directive you cannot use helpers, so there you would need to put the actual URL:
Since you probably have different CMS pages/blocks in different store views, this should not be a big problem.