Jump to content

Markup Google reCAPTCHA


flydev

Recommended Posts

  • 1 month later...
  • 4 months later...
On 9/30/2018 at 7:29 PM, Jonathan Lahijani said:

Does this work with FormBuilder?

That's a good question!  When I developed this module I didn't had FormBuilder in hand.

Now that I have a copy of this module, I will try to see if it works out of the box tomorrow. Stay tuned !

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 3 months later...

@gregory @Jonathan Lahijani

 

I tested it (but not with all the Custom Embed option), it works with the Custom Embed options C and D.

 

Use the following hook :

<?php namespace ProcessWire;
// the captcha module
$captcha = $this->modules->get("MarkupGoogleRecaptcha");
// hook FormBuilder
$forms->addHookBefore('FormBuilderProcessor::renderReady', function ($e) use ($captcha) 
{
    $form = $e->arguments(0);
    // do some custom check here ...
	
    if ($captcha->data_recaptcha_type === 'invisible') { // invisible captcha
        $captcha->render($form);
        $f = $this->modules->get("InputfieldHidden");
        $f->attr('id+name', 'login_submit');
        $f->value = 'login_submit';
        $form->append($f);
    } else { // recaptcha v2
        $f = $this->modules->get("InputfieldMarkup");
        $f->value = $captcha->render();
        foreach ($form->children as $field) {
            if ($field instanceof InputfieldSubmit) { // find the submit button
                $form->insertBefore($f, $field);      // insert reCAPTCHA before the submit button
            }
        }
    } 
});

 

 

I will test later to see if it's possible to embed the captcha into an IFrame.

 

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 months later...

Great Module, thanks.

I had some luck getting this to integrate with FormBuilder (method d) by using the following Form Builder hook to check the recaptcha before submitting:

$forms->addHookBefore('FormBuilderProcessor::processInputDone', function ($e) use ($captcha) {
$form = $e->arguments(0); // retrieve argument by number (zero based)
    // Check to see if recaptcha is true
	if ($captcha->verifyResponse() === true) {
        // This will process the form as usual
	} else { 
        // This will cause an error on form and stop processing 
        $form->error('Recaptcha is missing. Please tick I\'m not a robot and try again');
	}
});

More info on FormBuilder hooks: https://processwire.com/blog/posts/formbuilder-v34/

Cheers,

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
On 7/9/2016 at 11:45 AM, flydev said:

$captcha = $modules->get("MarkupGoogleRecaptcha"); // if submitted check response if ($captcha->verifyResponse() === true) { $out .= "Hi " . $input->post["name"] . ", thanks for submitting the form!"; } else { $out .= "<form method='post' action='{$page->url}'>\n" . "\t<input type='text' name='name'>\n" . $captcha->render() // render reCaptcha . "\t<input type='submit'>\n" . "</form>\n"; $out .= $captcha->getScript(); } echo $out;

Hello, can anyone sho thw right example ? The one with plain HTML doesnt work. 

And i really dont understand why ??  

Parse Error: syntax error, unexpected '}' (line 26 of /usr/www/xxxxx/site/templates/contact.php) 

 

Screenshot_7.jpg

Link to comment
Share on other sites

hi @iNoize

Looking at the error and the corresponding line (26) pointing to an empty line, I bet that you have some unwanted char in the code caused by the copy/pasta from the forum.

Try to open your file (contact.php) with another editor and clean weird chars. The plain HTML code should work as is.

Please have a read to those post :

 

 

 

 

 

If you still have an issue, ping me, I will check your templates.

Edited by flydev
Related Links
Link to comment
Share on other sites

I use the module regularly on sites (with Pw form API), but for the first time I needed to implement the module in a plain HTML form, but without any luck.

render() will not render anything and the getScript() does not add the script at the bottom of my form.

Does plain HTML still work?

 

Link to comment
Share on other sites

1 hour ago, flydev said:

Hi @KarlvonKarton  thanks for reporting it.

Can you tell me on which version of ProcessWire the module didn't work for you ? 

I did the same mistake as someone here above...  I did not echo the render() nor the getScript() ... (my form is in plain HTML and not in a var called $out, therefrom...)
It's working now. ;-)

  • Thanks 1
Link to comment
Share on other sites

  • 2 months later...

@flydev Great module.

It would be nice to have/force a different language or multi-language support. Right now Google renders the widget in what it thinks is you native language. For me that's Dutch, although the html lang="en", the site's default language and my browser is in English as well.

This could be done via a simple extra parameter in your module's getScript() and a "hl=langcode" setting, ie.:
<script src='https://www.google.com/recaptcha/api.js?hl=en'></script>
There's a list at https://developers.google.com/recaptcha/docs/language

Edit 1: Already figured out you can do this via the ->render("","en") method.
Edit 2: Somehow that's not working. But I expect it's Google's fault. It renders "en" but still displays it in Dutch.

image.thumb.png.46c33a2b020256f9ce6c2a50e2665de5.png

Edit 3: It was Google's fault. When you're logged in with your Google account in Chrome (aka 'sync') it will take the language of that account, no matter what language you asked the widget to be. Google always knows better™ ?

Edited by JFn
Solved
  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...
1 hour ago, abdulqayyum said:

Hello,

anyone can tell me how to "Markup Google reCAPTCHA" this module use with "LoginRegister" module.

if any one know please suggest me how i use it.

Thanks
AbdulQayyum.

Hi Abdul,

The solution is already in this thread ?

 

Link to comment
Share on other sites

  • 5 months later...

Dear All and @flydev ??

i am currently creating a contact form and wanted to use the Google recaptcha for this.

While implementing, I come across the following problem that I can not get solved...

My page code structure looks something like this:
 

<?php namespace ProcessWire; ?>

<div id='content-body'>

	<div class='uk-grid' data-uk-grid>
		<div id='content' class='uk-width-1-2@m'>
			<?=page()->body?>
		</div>

		<div id='contactform' class='uk-width-1-2@m'>
			<h3>Contact Form</h3>

			<?php // Google reCaptcha as Modul
			$captcha = $modules->get("MarkupGoogleRecaptcha");

			......

			// form was submitted so we process the form
			if($input->post->submit) {
				// Do cool stuff here  			 <<<<-------- I cannot get in here
			}
			?>

			<form class="uk-form-stacked" action="./" method="post">
			<div class="uk-margin">
				<label class="uk-form-label" for="name">Your Name <span class='uk-text-danger'>*</span></label>
				<div class="uk-form-controls">
					<input type="text" name="name" class="uk-input" value="<?php echo $name?>" placeholder="Your Name here">
				</div>
			</div>
			<div class="uk-margin">
				<label class="uk-form-label" for="email">Your Email-Adress <span class='uk-text-danger'>*</span></label>
				<div class="uk-form-controls">
					<input type="email" name="email" class="uk-input" value="<?php echo $email?>" placeholder="Email-Adress">
				</div>
			</div>
			....
			<div>
				<!-- Google reCaptcha code START -->
				<?php echo $captcha->render()?>
				<!-- Google reCaptcha code END -->
			</div>
			<div class="uk-margin">
				<input type="submit" name="submit" class="uk-button uk-button-default" value="Submit">
			</div>
		</form>

		<?php echo $captcha->getScript(); ?>


		</div>
	</div>
</div>

My problem is now, as soon as I insert this line of code "<?php echo $captcha->getScript(); ?>" on the page like this, my query "if($input->post->submit)" does NOT work anymore ....

HELP what is wrong here or where is my mistake?

Thanks
Ralf

Link to comment
Share on other sites

Hi @Ralf, sorry for the delay.

Instead of checking for the $input->post->submit, you should check if the response from the ReCaptcha is green.

So, instead of :

// form was submitted so we process the form
if($input->post->submit) {
   // Do cool stuff here  			 <<<<-------- I cannot get in here
}

Do :

// form was submitted so we process the form
if($captcha->verifyResponse() === true) {
   // Do cool stuff here  			 <<<<-------- you will get here now
}

 

Also, try to call the getScript() method on the end of your main markup page. I mean, just before the </body></html> tags. And to avoid calling the module every page request, you can for exemple, check if the current page's template is the contact one and then call the module's getScript() method :

<html>
  <head>
  </head>
  <body>

   ...
  
   <!-- other tiers scripts exemple -->
   <script src="jquery.js"></script>
   <script src="anotherscript.js"></script>
  
   <?php 
   if($page->template->name === 'contact-tpl') {
     // google recaptcha required script
     echo $modules->get("MarkupGoogleRecaptcha")->getScript();
   }
   ?>

</body>
</html>

 

Ping me if you need further help.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

reCaptcha v2 invisible doesn't seem to be working (or what am I to expect? I've never seen an example for the invisible version anyway – kind of ironic, I know).

I had reCaptcha v2 (the regular one) working just fine.

Now that I want to change it to invisible I…

  • added a new site-project in the reCaptcha-settings on google.com
  • selected "invisible" in this module's settings
  • and entered the newly created site key and secret key in this module's settings as well as in the div-attribute on the template.

refreshed the page (cleared cookies and cache just to be save) but I see no change whatsoever.

Thoughts?

Link to comment
Share on other sites

  • 3 weeks later...

Hi @flydev ?? I just installed the module and I'm getting this PHP Notice:

Notice: Undefined index: data_badge in C:\laragon\www\mysite\site\assets\cache\FileCompiler\site\modules\MarkupGoogleRecaptcha\MarkupGoogleRecaptcha.module on line 269

Replacing the 269 line with this, fixes the issue:

if(isset($data['data_badge'])) $f->attr('value', $data['data_badge']);

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...