Try the Demo

Magento Forum

   
Magento CE 1.7.0.2 how to add Mage Captcha to Contact Us form
 
fsj911
Jr. Member
 
Total Posts:  18
Joined:  2008-10-05
 

Quick outline how to add Magento builtin captcha mechanism to Contact Us default page.

1) To add Contact Us form to Admin/Config interface you could
a) find captcha/forms element in core_config table and add “contact_form” form name to the list
b) modify config.xml in /app/code/core/Mage/Captcha/etc
in - <frontend> - <areas> add something like

<contact_form>
  <
label>Contact Us</label
  </
contact_form>

and in - <always_for>

<contact_form>1</contact_form>

Also there\\\’s a miss for translation in the config file, so you should add this to the <frontend> section:

<translate>
 <
modules>
 <
Mage_Captcha>
 <
files>
  <default>
Mage_Captcha.csv</default> 
  </
files>
  </
Mage_Captcha>
  </
modules>
  </
translate>

Refresh cache. Login to Admin, and make sure Contact Us form is listed in CAPTCHA configuration

2) Modify captcha layout xml file in frontend/base/default (or your theme)
add this section

<contacts_index_index>
 <
reference name="contactForm">
 <
block type="core/text_list" name="form.additional.info">
 <
block type=\\\"captcha/captcha\\\" name=\\\"captcha\\\">
 <
reference name=\\\"head\\\">
  <
action method=\\\"addJs\\\">
  <
file>mage/captcha.js</file
  </
action>
  </
reference>
  <
action method=\\\"setFormId\\\">
  <
formId>contact_form</formId
  </
action>
 <
action method=\\\"setImgWidth\\\">
  <
width>230</width
  </
action>
<
action method=\\\"setImgHeight\\\">
  <
width>50</width
  </
action>
  </
block>
  </
block>
  </
reference>
  </
contacts_index_index>

Refresh cache

3) Modify form code in /frontend/base/default/template/contacts/form.phtml to add placeholder to render captcha layout
Add this line of code right before </ul>

<?php echo $this->getChildHtml(\\\'form.additional.info\\\'); ?>

4) FInal part - how to\\\"trap\\\" the post from the Contact Us form and check the captcha
Mage uses Events and Observer to achieve this, and it works very well for the forms/events they have implemented this feature. I was not able to find events specific for Contact Us form, so, IMHO the most obvious solution is to override indexController in code/core/Mage/Contacts/controllers and in the postAction method, inside try block add the following code:

$formId \\\'contact_form\\\';
           
$captchaModel Mage::helper(\\\'captcha\\\')->getCaptcha($formId);
           if (
$captchaModel->isRequired()) {
            
if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
                Mage
::getSingleton(\\\'customer/session\\\')->addError(Mage::helper(\\\'captcha\\\')->__(\\\'Incorrect CAPTCHA.\\\'));
                
$this->setFlag(\\\'\\\'Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCHtrue);
                
Mage::getSingleton(\\\'customer/session\\\')->setCustomerFormData($this->getRequest()->getPost());
                
$this->getResponse()->setRedirect(Mage::getUrl(\\\'*/*/\\\'));
                return;
            
}
           }

and add this \\\"utility method to the controller class:

protected function _getCaptchaString($request$formId)
    
{
        $captchaParams 
$request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return 
$captchaParams[$formId];
    
}

5) Refresh cache, and give it a try

 
Magento Community Magento Community
Magento Community
Magento Community
 
exposius
Jr. Member
 
Total Posts:  4
Joined:  2012-05-29
 

Hi, it works for me:-) Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Infolution_Biz
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2010-01-19
 

Or use this extension:

http://www.magentocommerce.com/magento-connect/ilcaptcha-8142.html

I’m the developer of the extension. It’s simple, just install and it works.

 
Magento Community Magento Community
Magento Community
Magento Community
 
takcassidy
Jr. Member
 
Total Posts:  5
Joined:  2012-11-13
 

Hi Thanks for this,

Just struggling a bit with part 4.
Not sure where to paste the code or whether it replaces code or is an addition. also the bit about controllers, where does that go.

Thanks
Tony

 
Magento Community Magento Community
Magento Community
Magento Community
 
keanokia
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2012-10-17
 

your a fucking awesome..., thanks it works, you save my life.

 Signature 

iStockphp | LifeSyle | Email:

 
Magento Community Magento Community
Magento Community
Magento Community
 
IonTrust
Jr. Member
 
Total Posts:  1
Joined:  2010-10-04
 

Works like a charm, thank you smile

*edit*

You can almost use the same code in order to add a captcha to the “mail to friend” form, much appreciated smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
maria_atwix
Sr. Member
 
Avatar
Total Posts:  125
Joined:  2012-01-23
 

Hi! Discussion of the issue is described here http://www.atwix.com/magento/captcha-in-magento/
welcome to look through this article and hope this information will be useful

 Signature 

Atwix.com - Magento Custom Development and Design

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spyyy
Jr. Member
 
Total Posts:  12
Joined:  2012-10-28
 
maria_atwix - 21 February 2013 01:26 AM

Hi! Discussion of the issue is described here http://www.atwix.com/magento/captcha-in-magento/
welcome to look through this article and hope this information will be useful

Hey, thanks for tutorial but it’s a little messy and finally i didnt get it working, eh.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dpeczek
Jr. Member
 
Total Posts:  1
Joined:  2011-10-08
 

It works but annoyingly for the user if they screw up the captcha the form is emptied…

 
Magento Community Magento Community
Magento Community
Magento Community
 
phongdevelopers
Jr. Member
 
Total Posts:  1
Joined:  2011-10-25
 

hi all ,everybody can write this module for me . i want it

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top