1

Lots of spam is being sent to this form. I need to add Google Recaptcha to it.

What is the PHP file that's responsible for receiving the contact POST at /contacts/index/post/ ?

After some search I couldn't find anything relevant except form.phtml which seems to be only responsible for displaying the form but not receiving the POST data.

1
  • did you get your solution? Aug 11, 2018 at 6:45

2 Answers 2

2

He is talking about Magento1 because there no contacts in Magento2

Magento1 = contacts/index/post

Magento2 = contact/index/post

So contacts/index/post is a controller contact form module :

contacts = frontname,

index = controllerName,

post = postAction().

If you want to add you captcha, so it's happening in form.phtml, you have nothing to do in controller.

To add GoogleRecaptcha in your contact form :

app/design/frontend/{package}/{themeName}/template/contacts/form.phtml

$sitekey ='site_key_here'; //you get it from googleRecaptcha V3
$secret ='secret_key_here'; //you get it from googleRecaptcha V3
...
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" onsubmit="return validateCaptcha()">
    ...
    <div class="g-recaptcha" id="recaptcha" data-sitekey="<?php echo $sitekey; ?>"></div> <!-- Google CAPTCHA-->
    <div id="error-g-captcha" style="display:none;"><?php echo $this->__('Please check the captcha validation !')?></div>
</form>

<script type="text/javascript">
//<![CDATA[
    var contactForm = new VarienForm('contactForm', true);

    // captcha validation
    function validateCaptcha() {
        var v = grecaptcha.getResponse();
        if(v.length == 0) {
            jQuery('#error-g-captcha').show();
            return false;
        } else {
            jQuery('#error-g-captcha').hide();
            return true;
        }
    }
//]]>
</script>

app/design/frontend/{package}/{themeName}/layout/local.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        ...
        <reference name="head">
           <action method="addItem"><type>external_js</type><name>https://www.google.com/recaptcha/api.js</name><params/></action>
        </reference>
    </default>
</layout>
0

For Magento 1: Goto to below location

app/code/core/Mage/Contacts/controllers/IndexController.php

You will find postAction() this is the function you are looking for.

Before changing anything please override that function to your custom module then change

OR

You can install 3rd party extension for captcha.

If you are using magento 2 then file location will be different.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.