tomtoump's solution to "Integration Google reCAPTCHA"

You can download the code here<\/a>.<\/p>\n

<?php<\/span>\n\nrequire<\/span>(<\/span>'recaptcha/autoload.php'<\/span>);<\/span>\n\n// Configuration<\/span>\n$secret<\/span> =<\/span> 'YOUR_SECRET'<\/span>;<\/span>\n\nif<\/span> (<\/span> !<\/span> empty<\/span>(<\/span>$_POST<\/span>[<\/span>'name'<\/span>])<\/span> &&<\/span>\n     !<\/span> empty<\/span>(<\/span>$_POST<\/span>[<\/span>'email'<\/span>])<\/span> &&<\/span> filter_var<\/span>(<\/span>$_POST<\/span>[<\/span>'email'<\/span>],<\/span> FILTER_VALIDATE_EMAIL<\/span>)<\/span>\n     !<\/span> empty<\/span>(<\/span>$_POST<\/span>[<\/span>'subject'<\/span>])<\/span> &&<\/span>\n     !<\/span> empty<\/span>(<\/span>$_POST<\/span>[<\/span>'message'<\/span>])<\/span> &&<\/span>\n     !<\/span> empty<\/span>(<\/span>$_POST<\/span>[<\/span>'g-recaptcha-response'<\/span>]))<\/span> {<\/span>\n\n    $response<\/span> =<\/span> $recaptcha<\/span>-><\/span>verify<\/span>(<\/span>$_POST<\/span>[<\/span>'g-recaptcha-response'<\/span>],<\/span> $_SERVER<\/span>[<\/span>'REMOTE_ADDR'<\/span>]);<\/span>\n\n    if<\/span> (<\/span>$response<\/span>-><\/span>isSuccess<\/span>())<\/span> {<\/span>\n        $to<\/span> =<\/span> 'web.prodaja@copy-electronic.hr'<\/span>;<\/span>\n        $subject<\/span> =<\/span> 'Web upit - Copy Electronic'<\/span>;<\/span>\n        $headers<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>From<\/span>:<\/span> $_POST<\/span>[<\/span>'email'<\/span>]<\/span>\\r\\n<\/span>&<\/span>quot<\/span>;<\/span>.<\/span>\n                   &<\/span>quot<\/span>;<\/span>Reply<\/span>-<\/span>To<\/span>:<\/span> $_POST<\/span>[<\/span>'email'<\/span>]<\/span>&<\/span>quot<\/span>;;<\/span>\n        $message<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>Ime<\/span>:<\/span> $_POST<\/span>[<\/span>'name'<\/span>]<\/span>\\n<\/span>&<\/span>quot<\/span>;<\/span>.<\/span>\n                   &<\/span>quot<\/span>;<\/span>E<\/span>-<\/span>pošta<\/span>:<\/span> $_POST<\/span>[<\/span>'email'<\/span>]<\/span>\\n<\/span>&<\/span>quot<\/span>;<\/span>.<\/span>\n                   &<\/span>quot<\/span>;<\/span>Naslov<\/span>:<\/span> $_POST<\/span>[<\/span>'subject'<\/span>]<\/span>\\n<\/span>&<\/span>quot<\/span>;<\/span>.<\/span>\n                   &<\/span>quot<\/span>;<\/span>Poruka<\/span>:<\/span> $_POST<\/span>[<\/span>'message'<\/span>]<\/span>&<\/span>quot<\/span>;;<\/span>\n\n        mail<\/span>(<\/span>$to<\/span>,<\/span> $subject<\/span>,<\/span> $message<\/span>,<\/span> $headers<\/span>);<\/span>\n    }<\/span>\n}<\/span>\n<\/pre><\/div>
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). ). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. php require('recaptcha/autoload.php'); // Configuration $secret = 'YOUR_SECRET'; if ( ! empty($_POST['name']) && ! empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ! empty($_POST['subject']) && ! empty($_POST['message']) && ! empty($_POST['g-recaptcha-response'])) { $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = "From: $_POST['email']\r\n". "Reply-To: $_POST['email']"; $message = "Ime: $_POST['name']\n". "E-pošta: $_POST['email']\n". "Naslov: $_POST['subject']\n". "Poruka: $_POST['message']"; mail($to, $subject, $message, $headers); } } ); } } I could help you more if you provided the snippet where you render the form. The general idea is to set the `data-callback` attribute on this element:
Add the `disabled` property to the submit button: And remove it when the callback is run: function captchaOk(){ $('#form-submit').prop('disabled', false); }
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = "From: $_POST['email']\r\n". "Reply-To: $_POST['email']"; $message = "Ime: $_POST['name']\n". "E-pošta: $_POST['email']\n". "Naslov: $_POST['subject']\n". "Poruka: $_POST['message']"; mail($to, $subject, $message, $headers); } } ); echo json_encode([ 'success' => true ]); } else { echo json_encode([ 'success' => false, 'error' => 'recaptcha' ]); } } I could help you more if you provided the snippet where you render the form. TheYou generalhave idea is to set the `data-callback` attribute on this element:
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop(); if (response.success) { $('#sky-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess(){ $('.g-recaptcha').removeClass('state-error').next().remove it when the callback is run: function captchaOk(){ $('#(); $('#form-submit').prop('disabled', false); }); }
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = "From: $_POST['email']\r\n". "Reply-To: $_POST['email']"; $message = "Ime: $_POST['name']\n". "E-pošta: $_POST['email']\n". "Naslov: $_POST['subject']\n". "Poruka: $_POST['message']"; mail($to, $subject, $message, $headers); echo json_encode([ 'success' => true ]); } else { echo json_encode([ 'success' => false, 'error' => 'recaptcha' ]); } } I could help you more if you provided the snippet where you render the form. You have to set the `data-callback` attribute on this element:
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop(); if (response.success) { $('#sky-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess(){ $('.g-recaptcha').removeClass('state-error').next().remove(); $('#form-submit').prop('disabled', false); }
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = "From: $_POST['email']\r\n". "Reply-To: $_POST['email']"; $message = "Ime: $_POST['name']\n". "E-pošta: $_POST['email']\n". "Naslov: $_POST['subject']\n". "Poruka: $_POST['message']"; mail($to, $subject, $message, $headers); echo json_encode([ 'success' => true ]); } else { echo json_encode([ 'success' => false, 'error' => 'recaptcha' ]); } } You have to set the `data-callback` attribute on this element:
captchaOk">
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop(); if (response.success) { $('#sky-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess">
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop(); if (response.success) { $('#sky-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess(){ $('.g-recaptcha').removeClass('state-error').next().remove(); $('#form-submit').prop('disabled', false); }
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. recaptcha = new \ReCaptcha\ReCaptcha($secret); $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = "From: $_POST['email']\r\n". "Reply-To: $_POST['email']"; $message = "Ime: $_POST['name']\n". "E-pošta: $_POST['email']\n". "Naslov: $_POST['subject']\n". "Poruka: $_POST['message']"; mail($to, $subject, $message, $headers); echo json_encode([ 'success' => true ]); } else { echo json_encode([ 'success' => false, 'error' => 'recaptcha' ]); } } You have to set the `data-callback` attribute on this element:
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop(); if (response.success) { $('#sky-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess(){ $('.g-recaptcha').removeClass('state-error').next().remove(); $('#form-submit').prop('disabled', false); }
You can download the code [here](https://dl.dropboxusercontent.com/u/55365/recaptcha/recaptcha.zip). Apart from checking the reCAPTCHA, I check every submitted variable because you should not rely only on JS. That's also the reason for doing the reCAPTCHA check on the server. '); // '); /** * Configuration $ */ $secret = 'YOUR_SECRET'; if ( ! empty($_POST['name']) && ! empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ! ) && ! empty($_POST['subject']) && ! empty($_POST['message']) && ! empty($_POST['g-recaptcha-response'])) { $recaptcha = new \ReCaptcha\ReCaptcha($secret); $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); if ($response->isSuccess()) { $to = 'web.prodaja@copy-electronic.hr'; $subject = 'Web upit - Copy Electronic'; $headers = " = 'From: $: ' . $_POST['email']\'] . "\r\n". "" . 'Reply-To: $: ' . $_POST['email']"; $']; $message = " = 'Ime: $: ' . $_POST['name']\'] . "\n". "" . 'E-pošta: $: ' . $_POST['email']\'] . "\n". "" . 'Naslov: $: ' . $_POST['subject']\'] . "\n". "" . 'Poruka: $: ' . $_POST['message']"; ']; mail($to, $subject, $message, $headers); echo json_encode([ 'success' => true ]); } else { echo json_encode([ 'success' => false, 'error' => 'recaptcha' ]); } } You have to set the `data-callback` attribute on this element:
Add the `disabled` property to the submit button: And replace/add this JS to your scripts file : $(function() { $('#sky-form').validate({ /* rules messages */ submitHandler: function(form) { $(form).ajaxSubmit({ beforeSend: function() { l.start(); $('#form-submit').attr('disabled', true); }, success: function(response) { l.stop();
if (( $_POST['copy'] == 'on' ) { mail($_POST['email'], $subject, $message, $headers); } } } Set the data-callback attribute on this element:
And replace/add this JS to your scripts file : $(function() { // Validation $("#sky-form").validate({ // Rules for form validation rules: { /* * ... */ 'g-recaptcha-
response.success) { $('#sky': { required: true } }, // Messages for form validation messages: { /* * ... */ 'g-form').addClass('submited'); } else if (response.error == 'recaptcha') { $('.g-response': { required: 'YOUR_ERROR_MESSAGE' } }, // submitHandler // errorPlacement }); }); function recaptchaSuccess(){ $('#g-recaptcha') .addClass('state-error') .after('Invalid reCAPTCHA'); } } }); }, /* errorPlacement */ }); }); function recaptchaSuccess(){ $('.g-recaptcharesponse').removeClass('state-error').next().remove(); $('#form-submit').prop('disabled', false); } valid(); }

User: tomtoump

Question: Integration Google reCAPTCHA

Back to question