There is a popup feedback plugin, in the form in which an action has already been assigned (sending form fields to email)

<div class="callback-form {$inline_container}">{$label}{$description}<form id="send" class="clearfix callback-form-container" action="{$form_action}" method="POST"> //inputs <button id="" data-sitekey="" ...></button></from></div> 

Screwed to the form of recaptcha, I had to change the action with

 $form_action = get_site_url() . '/?form_action=email'; 

on

 $form_action = get_site_url() . '/captcha.php'; 

reaptcha code

 <?php require '1.php'; $recaptcha = $_POST['g-recaptcha-response']; $object = new Recaptcha(); $response = $object->verifyResponse($recaptcha); if(isset($response['success']) and $response['success'] != true) { exit("An Error Occured and Error code is :".$response['error-codes']); }else { } 

Now, when you submit the form, naturally, /captcha.php opens with the text "Correct Recaptcha", this is where it ends, the email does not go away

How can I make the form go after checking the captcha, without redirection to captcha.php

Help me please.

  • one
    Add the contents of captcha.php to your handler, why did you create a separate one? And at the moment when the data has passed your validation and captcha validation, send a letter, save it to the database, etc. You need to combine the code simply. - DaemonHK
  • @DaemonHK, thanks. I correctly understand that require "captcha.php"; here not to get off? Just validation takes place in JS, so I still have no idea how to integrate. - Mikhail
  • Validation should first come from the server. You can Aayax receive a response from Google captcha, check the answer to js and do further validation on it. - DaemonHK
  • @DaemonHK, I would still like the captcha check to take place not by the fact of the opening of the form, but by clicking submit, i.e. after validation. But it is impossible to get to work correctly. If captcha works, then validation does not work and vice versa. Below I will leave the code, please see if it is not difficult. - Mikhail

3 answers 3

validation

 jQuery(document).ready(function($) { $('.callback-form-container').submit(function() { var formInputs = $(this).find('.validate'); var errors = ''; $(formInputs).each(function() { if($.trim(this.value) == '') { fieldLabel = $(this).parent().find('span.label-text').html(); errors += '- ' + fieldLabel + '\n'; } }); if(errors.length > 0) { alert('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π·Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ поля:\n\n' + errors); return false; } else { $('.submit-button').val('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, ΠΏΠΎΠ΄ΠΎΠ΄ΠΆΠΈΡ‚Π΅...'); $('.submit-button').attr('disabled', 'disabled'); return true } }); }); }); 

reCaptcha.php

 if(isset($_POST['g-recaptcha-response'])) { $result = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=6(secret code)O&response=$_POST["g-recaptcha-response"]&remoteip=$_SERVER["REMOTE_ADDR"]'), TRUE); if($result['success'] == 1) { console.log("Ok"); } else { grecaptcha.reset(); } } 

ajax, which fails to place correctly:

  function onSubmit(token) { document.getElementById("send").submit(); $.ajax({ type: "POST", url: "/reCaptcha.php", data: f.serialize(), dataType: "json", }); } 

    Something like this:

     jQuery(document).ready(function($) { var form=$('.callback-form-container'); form.submit(function() { var formInputs = $(this).find('.validate'); var errors = ''; $(formInputs).each(function() { if($.trim(this.value) == '') { fieldLabel = $(this).parent().find('span.label-text').html(); errors += '- ' + fieldLabel + '\n'; } }); $.ajax({ type: "POST", url: "/reCaptcha.php", data: form.serialize(), dataType: "json", error:function(){ // }, success:function(result){ if(errors.length > 0) { alert('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π·Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ поля:\n\n' + errors); }else{ $('.submit-button').val('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, ΠΏΠΎΠ΄ΠΎΠ΄ΠΆΠΈΡ‚Π΅...'); $('.submit-button').attr('disabled', 'disabled'); //Π΅Ρ‰Π΅ ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ дСйствия } } }); }); }); 

    The general essence: Validate the data, then send an ajax request for a captcha, if everything is OK, then send the data further, if not, then we show errors.

    • Alas, the result is the same - verification passes, no validation, i.e. the form can not be filled at all, it is sent without any problems. - Mikhail
    • You should also check the result from the captcha - DaemonHK
    • added to the error: function check (published below). Tell me, what is it for? After all, there is a check in captcha.php or is it different? My question is only in case of successful verification, if not what messages are needed, but just continue, what should I add? and what can / should be added where you designated "// some more actions", - Mikhail
    • That, the check that you added to js, ​​is responsible for the validation of the form, in captcha its own check, I would generally push yours on the server side to check the server all at once. What is the function CheckSend? - DaemonHK
    • I apologize, instead of checkSend should be beforeSend. The success function is also added, you have already registered it. Unfortunately, I still can’t do that validation and captcha work. As for verification, I understand that everything is better done through the server, but I will not override it. If you still have something to tell, I will be grateful. - Mikhail
     jQuery(document).ready(function($) { var form=$('.callback-form-container'); form.submit(function() { var formInputs = $(this).find('.validate'); var errors = ''; $(formInputs).each(function() { if($.trim(this.value) == '') { fieldLabel = $(this).parent().find('span.label-text').html(); errors += '- ' + fieldLabel + '\n'; } }); $.ajax({ type: "POST", url: "/reCaptcha.php", data: form.serialize(), dataType: "json", CheckSend: function(){ $("#status").html("logging in..."); }, success: function(response){ $("#alert").html(response.text); if(response.type=="success"){ //Ρ‡Ρ‚ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ? } }, error: function(){ $("#alert").html("Failed."); }, success:function(result){ if(errors.length > 0) { alert('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π·Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ поля:\n\n' + errors); }else{ $('.submit-button').val('ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, ΠΏΠΎΠ΄ΠΎΠ΄ΠΆΠΈΡ‚Π΅...'); $('.submit-button').attr('disabled', 'disabled'); //Π΅Ρ‰Π΅ ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ дСйствия } } }); }); });