Organizational Research By

Surprising Reserch Topic

magento add captcha to custom contact form


magento add captcha to custom contact form  using -'magento,customization,captcha,contact-form'

I created a new custom contact form in Magento website, I am trying to add captcha at the end of it.

I have added to app/code/core/Mage/Captcha/etc/config.xml

                <customcontacts>
                    <label>Custom Contact Form</label>
                </customcontacts>


And I added to app/design/frontend/base/default/layout/captcha.xml

    <customcontacts>
    <reference name="customcontacts">
        <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>customcontacts</formId></action>
                <action method="setImgWidth"><width>230</width></action>
                <action method="setImgHeight"><width>50</width></action>
            </block>
        </block>
    </reference>
</customcontacts>


And I added to my form this code:

<script type="text/javascript">
//<![CDATA[
$('form-validate-captcha').captcha.refresh($('catpcha-reload'));
//]]>




And I added this code to show the Captcha:

<div><?php echo $this->getChildHtml('captcha'); ?></div>


Then I enabled Captcha in Configuration > Customer Configuration > CAPTCHA and I choose "Custom Contact Form" then "Displaying Mode" to "Always".

But I still didn't get the Captcha at my custom form yet.

I hope I was clear

Thanks
    

asked Oct 7, 2015 by atulpariharmca
0 votes
142 views



Related Hot Questions

2 Answers

0 votes

if ,you want to add magento own captcha to contact us form ,then follow below step.....

Donot write code in code file create an extension

Step1: Create module control file

path app/etc/modules/Amit_Captchaown.xml add below code





true
local






Step2:

Create module config.xml file in app/code/local/Amit/Captchaown/etc/config.xml.Rewrite model of zend and captcha controllers and here code

 



1.0.0



    
    
        
        Amit_Captchaown_Model_Zend
        
        
    


    
    
    
    
    
    
    
    
    

    
    
    
    1
    
    
    






Amit_Captchaown






Step3:

Override Mage_Captcha_Model_Zend file _isUserAuth() function create Zend.php file in app/code/local/Amit/Captchaown/Model/Zend.php.In this function you need to only comment function code.

getStore()->isAdmin()
? Mage::getSingleton(‚Äėadmin/session‚Äô)->isLoggedIn()
: Mage::getSingleton(‚Äėcustomer/session‚Äô)->isLoggedIn();*
*/
}

}

Step4: Then add some code in contacts.xml path app/design/frontend/our package/your template/layout

add and modify code is

    
        
        
            Contact Us
        
        
            
            Contact Us
        
       
            

            
            
            
            mage/captcha.js
            
            contact_us
            230
            50
            
            



    

Step5:

Then add codegetChildHtml('form.additional.info'); ?> to form.phtml path app/design/frontend/our package/your template/tempate/contacts.

Step6:

Now you need to override contact us controller create file in app/code/local/Amit/Captchaown/controllers/IndexController.php....

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;
}

$formId ='contact_us';
$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_DISPATCH, true);

Mage::getSingleton('customer/session')->setCustomerFormData($this->getRequest()->getPost());
$this->getResponse()->setRedirect(Mage::getUrl('*/*/'));
return;
}
}

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

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

}

Now just enable captcha in contact us form go to your admin panel setting sytem->configuration->customer configuration->Captcha

answered Oct 7, 2015 by deepak gupta
0 votes

I came up with a variation of Amit Bera's answer that's simple to implement: the "are you people" written question and answer.

In Admin -> System -> Custom Variables create two variables:

enter image description here

Edit your template file for the contact form (might be somewhere else if you purchased a template) app/design/frontend/base/default/template/contacts/form.phtml

Near the top, just under the header, I added these lines to retrieve the custom variables information and then in the script tags, added the custom validation function to the Validation class declared in prototype JS. By having the javascript in the template file, you are able to dynamically set the question's answer variable via php.

setStoreId(Mage::app()->getStore()->getId())
    ->loadByCode('contact_people_question')
    ->getValue('text');
$confirm_people_answer = Mage::getModel('core/variable')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCode('contact_people_answer')
    ->getValue('text');
?>

Further down in the same template/contacts/form.phtml file, add in the new field to the form itself:

  • Note the validate-people text is the same in attribute as well as in the javascript function we added above.

    End result: enter image description here

    answered Oct 7, 2015 by tushar2k6

    ...