Does this work if the form is filled in correctly? This was the problem I'm struggling with:

$mail = wireMail();
$mail->to($contactFormRecipient)->from($email, $name)->subject('Woodenfox design website form submission')->bodyHTML($message);if($mail->send()){// at "/contact/" this runs
$session->sent =true;
$session->flashMessage ="Thanks for your message! I will get back to you shortly.";
$session->redirect($pages->get($contactPageID->url));}else{// at "/contact/#form-top" this runs, presumably because of the initial// problem of variables not persisting at this new URL
$session->flashMessage ="Sorry, an error occured. Please try again.";}

open console and navigate to processwire root folder (where composer.json file is)
on this step i assume you have already setup composer for your project, otherwise google it
run the following command:

composer update

this will create a vendor folder (if it does not already exist) and download valitron and google recaptcha libraries.

Then open your contact-page template file(usually named contact.php inside your templates directory) and add the following:
* Note: The form below uses bootstrap 3.0.0 css, so if you are using something else you need to make the appropriate changes.

Next create a file inside you templates directory with name: _contact-controller.php:
and set the required variables($googleSiteKey, $contactFormRecipient, $contactPageID)

<?php
namespaceProcessWire;/**
* here we include Valitron & Google recaptcha libraries
* make sure the path is correct in your template
*/
include(dirname(__FILE__)."/../../vendor/vlucas/valitron/src/Valitron/Validator.php");
include(dirname(__FILE__).'/../../vendor/google/recaptcha/src/ReCaptcha/ReCaptcha.php');/**
* here we add the form field values to Valitron
*/
$v =new \Valitron\Validator(array('name'=> $sanitizer->text($input->post->name),'email'=> $sanitizer->email($input->post->email),'message'=> $sanitizer->text($input->post->message),));/**
* validation rules set for each form field
* For more details on Valitron/Validator usage visit:
* https://github.com/vlucas/valitron
*/
$v->rule('required',['name','email','message']);
$v->rule('lengthMin','name',5);
$v->rule('email','email');/**
* set Google recaptcha site-key & secret-key
* create a new key from: https://www.google.com/recaptcha/admin
*/
$googleSiteKey ='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$googleSecretKey ='YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY';/**
* set the email of the contact form recipient(usually the website owner)
*/
$contactFormRecipient ='your@company.com';/**
* set the id of contact-page in order to redirect there when the form is sent
*/
$contactPageID ='1045';//here we check whether the 'name' field exists inside post variables (which means the form is posted)if($input->post->name){//if fields validation passesif($v->validate()){
$reCaptcha =new \ReCaptcha\ReCaptcha($googleSecretKey);
$resp = $reCaptcha->verify($input->post->{'g-recaptcha-response'}, $_SERVER["REMOTE_ADDR"]);//if google-recaptcha validation passesif($resp->isSuccess()){//This is the HTML message
$message ='
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form | '. $input->post->name .'</title>
</head>
<body>
<p>'. $input->post->message .'</p>
</body>
</html>';//here we send the form to $contactFormRecipient
wireMail($contactFormRecipient, $input->post->email,"Contact Form | ". $input->post->name, $message);//here we set a flash-message to notify the user that the form was successfully sent
$session->flashMessage ='Thank you for your message! We will get in touch with you shortly.';//save in session that the form is sent
$session->sent =true;//finally redirect user to contact-page
$session->redirect($pages->get($contactPageID)->url);}else{//self explain
$session->flashMessage ='Error while validating you are not a robot!';}}}?>

Thats all!
You now have a simple contact-form working with captcha and field validation!
I would be more than happy to help anyone having problems on the setup.

I always get call to a member function errors(), on a non-object. Cannot solve it. Any help appreciated

Share this post

Link to post

Share on other sites

Did you try putting the namespace at the top of the _contact-controller.php file?

What I reckon is $v is a Valitron object, and your error is complaining about a 'non-object' being referenced (trying to call a method called errors() on it). $v is in _contact-controller.php and your calling a method on it in contact.php. These two files don't 'know' about each other (even though you have the namespace in header.php). Maybe unnecessary, but to avoid things like this (undefined functions is another one that crops up) I just add the namespace to the top of every template. It's not that much hassle as I never have tonnes of templates anyway.

Therefore I would think that adding the namespace to _contact-controller.php would solve your issue.

Now I might be wrong but I'd try this if you haven't already. I can't see anything wrong with the code at a glance, especially if you copied it from a working example. The only difference I see is that yours doesn't have the namespaces.

1

1

Share this post

Link to post

Share on other sites

Did you try putting the namespace at the top of the _contact-controller.php file?

What I reckon is $v is a Valitron object, and your error is complaining about a 'non-object' being referenced (trying to call a method called errors() on it). $v is in _contact-controller.php and your calling a method on it in contact.php. These two files don't 'know' about each other (even though you have the namespace in header.php). Maybe unnecessary, but to avoid things like this (undefined functions is another one that crops up) I just add the namespace to the top of every template. It's not that much hassle as I never have tonnes of templates anyway.

Therefore I would think that adding the namespace to _contact-controller.php would solve your issue.

Now I might be wrong but I'd try this if you haven't already. I can't see anything wrong with the code at a glance, especially if you copied it from a working example. The only difference I see is that yours doesn't have the namespaces.

Hi @SamC Thank you for your help, I put namespace Processwire on both, contact.php and _contact-controller.php it all worked. I just had some design problems thus my page won't load properly. I guess I have to take more attention to frontend as well Thank you again!

Share this post

Link to post

Share on other sites

Thank you @SamC for the great help. Now it seems that when I enter all the information I get an error from Google ReCaptcha. Whenever I enter all the right credentials it throws me "Error while validating you are not a robot!"

I tried to re-entering the information so many times but no luck.

Share this post

Link to post

Share on other sites

Did you complete the recaptcha too? i.e. ticking the box and "Select all images with blah blah..." before submitting? The error comes from somewhere in you _contact_controller.php file, the line where the check for validation happens. Goes a bit like this:

// if form has been submittedif($input->post->sendMe){// if fields are filled out correctlyif($v->validate()){// if recpatcha is successfulif($captcha->verifyResponse()===true){// <<<<<<<<<<<<<<<<<<<<<<<< YOU'RE FAILING HERE// if mail has been sent successfullyif($mail->send()){
$session->sent =true;
$session->flashMessage ="Thanks for your message! I will get back to you shortly.";
$session->redirect($pages->get($contactPageID)->url);}else{// problem sending mail
$session->flashMessage ="Sorry, an error occured. Please try again.";}}else{// problem with recaptcha
$session->flashMessage ='Recaptcha must be complete.';// <<<<<<<<<<<<<<<<<<<<<<<< SO ELSE STATEMENT RUNS}}else{// problem with fields
$session->flashMessage ='Please fill out the fields correctly.';}}

Share this post

Link to post

Share on other sites

Did you complete the recaptcha too? i.e. ticking the box and "Select all images with blah blah..." before submitting? The error comes from somewhere in you _contact_controller.php file, the line where the check for validation happens. Goes a bit like this:

// if form has been submittedif($input->post->sendMe){// if fields are filled out correctlyif($v->validate()){// if recpatcha is successfulif($captcha->verifyResponse()===true){// <<<<<<<<<<<<<<<<<<<<<<<< YOU'RE FAILING HERE// if mail has been sent successfullyif($mail->send()){
$session->sent =true;
$session->flashMessage ="Thanks for your message! I will get back to you shortly.";
$session->redirect($pages->get($contactPageID)->url);}else{// problem sending mail
$session->flashMessage ="Sorry, an error occured. Please try again.";}}else{// problem with recaptcha
$session->flashMessage ='Recaptcha must be complete.';// <<<<<<<<<<<<<<<<<<<<<<<< SO ELSE STATEMENT RUNS}}else{// problem with fields
$session->flashMessage ='Please fill out the fields correctly.';}}

Yeah I put the tick and went through all of its procedures one by one. Still no luck. I have tried it almost 100 times, still nothing. Here is my _contact-controller.php code

//here we check whether the 'name' field exists inside post variables (which means the form is posted)
if ($input->post->name) {
//if fields validation passes
if ($v->validate()) {
$reCaptcha = new \ReCaptcha\ReCaptcha($googleSecretKey);
$resp = $reCaptcha->verify($input->post->{'g-recaptcha-response'}, $_SERVER["REMOTE_ADDR"]);
//if google-recaptcha validation passes
if ($resp->isSuccess()) {
//This is the HTML message
$message = '
<html><head><metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/><title>Contact Form | ' . $input->post->name . '</title></head><body><p>' . $input->post->message . '</p></body></html>';
//here we send the form to $contactFormRecipient
wireMail($contactFormRecipient, $input->post->email, "Contact Form | " . $input->post->name, $message);
//here we set a flash-message to notify the user that the form was successfully sent
$session->flashMessage = 'Thank you for your message! We will get in touch with you shortly.';
//save in session that the form is sent
$session->sent = true;
//finally redirect user to contact-page
$session->redirect($pages->get($contactPageID)->url);
} else {
//self explain
$session->flashMessage = 'Error while validating you are not a robot!';
}
}
}
?>

I found this way easier. You fill out the secret key and stuff in the module instead of pasting it into your php file.

Thank you @SamC I have figured it out, I am not using a module, I didn't quite get the module version of it so I tried the classic way of @giannisok, I mixed the google recaptcha keys. I fixed them and it all worked correctly, Thank you, I jut have one little question in my mind, how to get the beautiful message to the email, because as I can see the html code cannot be rendered and the messages come all mixed up and unwieldy

Share this post

Link to post

Share on other sites

how to get the beautiful message to the email, because as I can see the html code cannot be rendered and the messages come all mixed up and unwieldy

Not sure I'm following you here. Have you got a screenshot or something of what your message looks like when you receive it? I can only guess what mixed up and unwieldy means (to you). I presume you mean this:

Share this post

Link to post

Share on other sites

Not sure I'm following you here. Have you got a screenshot or something of what your message looks like when you receive it? I can only guess what mixed up and unwieldy means (to you). I presume you mean this:

Not the prettiest, but to be honest, I only care about the message content! Maybe just fiddle around with your $message variable here.

Yes, sorry for my language, I couldn't explain it in a better way I have tried both ways, Like yours as well but still I can see the html code in my email. so I omitted the html tags and did it like this

if ($v->validate()) {
$reCaptcha = new \ReCaptcha\ReCaptcha($googleSecretKey);
$resp = $reCaptcha->verify($input->post->{'g-recaptcha-response'}, $_SERVER["REMOTE_ADDR"]);
//if google-recaptcha validation passes
if ($resp->isSuccess()) {
$name = $input->post->name;
$email = $input->post->email;
$message = $input->post->message;
$message = "There is a message from {$name} with the email {$email}
The message is: '{$message}'";
//here we send the form to $contactFormRecipient
wireMail($contactFormRecipient, $input->post->email, "Pak Nesil | " . $input->post->name, $message);
//here we set a flash-message to notify the user that the form was successfully sent
$session->flashMessage = 'Thank you for your message! We will get in touch with you shortly.';
//save in session that the form is sent
$session->sent = true;
//finally redirect user to contact-page
$session->redirect($pages->get($contactPageID)->url);
} else {
//self explain
$session->flashMessage = 'Error while validating you are not a robot!';
}
}
}

So it worked for me, otherwise gmail shows all the html markup which is annoying.

Share this post

Link to post

Share on other sites

$message = $input->post->message;
$message ="There is a message from {$name} with the email {$email}
The message is: '{$message}'";

It looks like you're taking the message from the field, then overwriting it, so it'll always be the same, just with a different name and email. What about the actual message they wrote? Just tested this and $message does actually get included in the new variable. I thought it would be overwritten. Ignore that last bit :-p

There is also a difference in how single and double quotes work in PHP, that may explain why you're seeing the html tags.

Similar Content

How To Install
Download the zip file at Github or from the modules repository
Drop the module files in /site/modules/MarkupGoogleRecaptcha
In your admin, click Modules > Refresh
Click "install" for "MarkupGoogleRecaptcha"
Official install/uninstall doc: http://modules.processwire.com/install-uninstall/

API
You must create an API key prior to use this module. Goto https://www.google.com/recaptcha/admin to create your own. Next, add the API keys information to the module's settings.