0

I've tried googling and searching on here but I'm still receiving spam emails.

I am using formbuilder extension to create forms.

Below is my code.

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
window.onload = function() {
  var recaptcha = document.forms["custom_form"]["g-recaptcha-response"];
  recaptcha.required = true;
  recaptcha.oninvalid = function(e) {

    alert("Please complete the captcha");
   }
}
</script> 

<?php
$formIdfromCms = $this->getFormId();
$formIdfromUrl = $this->getRequest()->getParam('id');
$passFormId = $formIdfromCms ? $formIdfromCms : $formIdfromUrl;

$helper = Mage::helper('formbuilder');
$session = Mage::getSingleton('core/session');
if ($helper->isEnabled()): ?>
    <div class="formbuilder">
        <?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>
        <div class="formbuilder-form">
        <?php
            //$passFormId = intval($this->getRequest()->getParam('id'));
            $currentForm = $currentFormFieldsCollection = array();
            $currentForm = $this->getCurrentFormUsingBlock($passFormId);
            if(isset($currentForm) && $currentForm!=NULL && count($currentForm)):
                $titleImage = Mage::getBaseUrl('media').$currentForm->getTitleImage();
                $currentFormFieldsCollection = $this->getCurrentFormFields($passFormId);
                if(count($currentFormFieldsCollection)):
                    $currentFormId = $currentForm['forms_index'];
                    $formBgcolor = $currentForm['bgcolor'];
                    $formRedirectUrl = Mage::getUrl('formbuilder/index/formsubmit');
                    $submitButtonText = $currentForm['submit_text'];
                    if(!$submitButtonText)
                        $submitButtonText = "Submit";
                    ?>
                    <?php //echo $this->getLayout()->createBlock('core/template')->setTemplate('formbuilder/fields_validation.phtml')->toHtml(); ?>
                    <script type="text/javascript">
                    function validateOptionsCallback(elmId, result)
                    {
                        var container = $(elmId).up('ul.options-list');
                        if (result == 'failed') {
                            container.removeClassName('validation-passed');
                            container.addClassName('validation-failed');
                        } else {
                            container.removeClassName('validation-failed');
                            container.addClassName('validation-passed');
                        }
                    }
                    </script>               
                    <?php if($currentForm['title_image']): ?>
                        <div class="title_image">
                            <h1><?php echo "<img src='$titleImage' height='100' width='900' />" ?></h1>
                        </div>
                    <?php endif; ?>
                    <form method="post" id="custom_form" action="<?php echo $formRedirectUrl ?>" name="custom_form" enctype="multipart/form-data">
                        <div class="fieldset">
                            <h2 class="legend">
                                <?php echo $currentForm->getTitle(); ?>
                            </h2>
                            <?php if($currentForm['header_content']): ?>
                                <div class="header_content">
                                    <?php echo $currentForm['header_content']; ?>
                                </div>
                            <?php endif; ?>
                            <ul class="form-list product-options">
                                <?php
                                    foreach ($currentFormFieldsCollection as $key => $field)
                                    {
                                        $functionName = "get".ucfirst($field['previous_group'])."Html";
                                        echo $this->$functionName($field);
                                    }
                                ?>
                            </ul>
                            <?php if($currentForm['footer_content']): ?>
                                <div class="footer_content">
                                    <?php echo $currentForm['footer_content']; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="g-recaptcha" data-sitekey="xxxxxxxxxx"></div>
                        <div class="buttons-set">
                            <p class="required">* Required Fields</p>
                            <!-- <input type="text" style="display:none !important;" value="" id="hideit" name="hideit"> -->
                            <input type="hidden" name="current_form_id" id="current_form_id" value="<?php echo $currentFormId ?>">
                            <button class="button" title="<?php echo $this->__($submitButtonText) ?>" type="submit"><span><span><?php echo $this->__($submitButtonText) ?></span></span></button>
                        </div>
                    </form>             
                    <script type="text/javascript">
                        var myForm = new VarienForm('custom_form', true);
                    </script>
                    <style type="text/css">
                        <?php
                            $formStyle = "#custom_form .fieldset, #custom_form .form-list {background-color: ".$formBgcolor."}";
                            echo $formStyle;
                        ?>
                    </style>
                <?php else: ?>
                    <p class="note-msg"><?php echo $this->__('There are no fields enabled/available in the requested form.') ?></p>
                <?php endif; ?><!--if(count($currentFormFieldsCollection))-->
            <?php endif; ?><!--if(isset($currentForm))-->
        </div><!--formbuilder-form-->
    </div><!--formbuilder-->
<?php endif; ?> 
<script src='https://www.google.com/recaptcha/api.js?onload=reCaptchaCallback&render=explicit'></script>

I added the below piece at the top

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
window.onload = function() {
  var recaptcha = document.forms["custom_form"]["g-recaptcha-response"];
  recaptcha.required = true;
  recaptcha.oninvalid = function(e) {

    alert("Please complete the captcha");
   }
}
</script> 

The required script, I got from this link

After that I added this div before the submit button

<div class="g-recaptcha" data-sitekey="xxxxxxxxxx"></div>

The code is based off of the auto render code that google provided here.

I cleared all cache as well but somehow I am still receiving spam emails. Is it because the captcha is not being validated? If so, how exactly do I do that? I've read the documentation but I didn't quite understand it. I'm also not very strong in JS.

Please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.