Wednesday, 23 April 2014

How to keep the form filled after an error in Magento

Developers often create custom modules as per the requirements in Magento. A distinct form is created for each module to get information about the users in any Magento Ecommerce store. There is a possibility of occurring error when that form is submitted for the specific process. If error occurs the user is redirected back to same form but sometimes the form becomes empty when the user tries to fill the data with errors. It generates the negative impression in user’s mind and due to this silly mistake; you can lose your profitable customer visiting your Magento Ecommerce store. The actual situation should be occurred like the data in submitted form must be displayed again when redirecting back for error correction, so the user only need to modify where there is an error. So I have written this blog post to improve user experience and development skills.
Follow the steps to implement this functionality.
Step1: In the Post action of the particular form, put the following line of code anywhere. These lines of codes are used to save the data that is already entered by the user.



$data = $this->getRequest()->getPost()

Mage::getSingleton('core/session')->setData('your_form_id',new Varien_Object($data));
Your_form_id → The form ID which you have provided or filled in the form.

Step2:Put the proper coding for redirecting user to that form if any error occurs during the code execution into the above post action. For example like this.

$this->_redirectUrl($this->getRequest()->getServer('HTTP_REFERER'));
You can also use other method just for redirecting user on same page when error occurs.
 Step3:When User tries to refill the form data, the submitted fields must be displayed on the form to the user.For that you should include following lines of code.


$formdata = Mage::getSingleton('core/session')->getData('your_form_id ');
$formdata = $formdata ? $formdata : new Varien_Object();
Every field of the form must be set with following format into value property.
1
<b>value="<?php echo $formdata->getYourFieldName()?>"</b>
 Small Example:











<?php

$formdata = Mage::getSingleton('core/session')->getData('form-validate');

$formdata = $formdata ? $formdata : new Varien_Object();           ?>

<form action="[Your URL]" method="post" id="form-validate">

<input type="text" name="firstname" 
value<b>="<?php echo $this->htmlEscape($formdata->getFirstname()) ?></b>"
 id="firstname" />

</form>

Your valuable comments are always welcomed.

No comments:

Post a Comment