A
A
Alexander AS2019-06-02 22:15:15
JavaScript
Alexander AS, 2019-06-02 22:15:15

How to write a script correctly so that recaptha works with ajax on modx evo?

Good evening everyone!
Guys, please tell me how to deal with the ajax script in my case, or rather, google recaptha does not work for me.
Chunk:

<form class="become-partner__form form" action="become-partner" method="post">
  [+form.messages+]
  <input type="hidden" name="formid" value="partner">
  <div class="become-partner-block">
    <p class="become-partner-block__title">Как вас зовут?</p>
    <input type="text" class="input become-partner-block__input [+user_name.errorClass+][+user_name.requiredClass+]" name="user_name" placeholder="Ваше имя" value="[+user_name.value+]">
    [+user_name.error+]
  </div>
  <div class="become-partner-block">
    <p class="become-partner-block__title">Навание компании</p>
    <input type="text" class="input become-partner-block__input [+company_name.errorClass+][+company_name.requiredClass+]" name="company_name" placeholder="Ваша компания" value="[+company_name.value+]">
    [+company_name.error+]
  </div>
  <div class="become-partner-block">
    <p class="become-partner-block__title">Телефон для связи</p>
    <input type="tel" class="input become-partner-block__input [+phone.errorClass+][+phone.requiredClass+]" name="phone" placeholder="+7 (" value="[+phone.value+]">
    [+phone.error+]
  </div>
  <div class="become-partner-block">
    <p class="become-partner-block__title">Чем занимается ваша компания?</p>
    <input type="text" class="input become-partner-block__input [+work_company.errorClass+][+work_company.requiredClass+]" name="work_company" placeholder="Род деятельности" value="[+work_company.value+]">
    [+work_company.error+]
  </div>
  <div class="become-partner-block">
    <p class="become-partner-block__title">Ваше предложение:</p>
    <textarea class="become-partner-block__textarea [+message.errorClass+][+message.requiredClass+]" name="message" placeholder="Просто начните заполнять форму">[+message.value+]</textarea>
    [+message.error+]
  </div>
  <div class="become-partner-block">
    <div class="captha-block [+g-recaptcha-response.errorClass+] [+g-recaptcha-response.requiredClass+]">
      [+captcha+]
      [+g-recaptcha-response.error+]
    </div>
    <button type="submit" class="become-partner-block__button button">Отправить</button>
  </div>
</form>


case 'become-partner':
    $result = $modx->runSnippet('FormLister', array(
      'formid' => 'partner',
      'to' => $modx->config['emailsender'],
      'errorClass'=> ' has-error',
      'requiredClass'=> ' has-error',
      'config' => 'partner:/assets/validators/',
      'parseDocumentSource' => '1',
      'debug'=> '2',
      'messagesTpl' => '@CODE:<div class="form-messages alert alert-danger">[+messages+]</div>',
      'errorTpl'=>'@CODE: <div class="error">[+message+]</div>',
      'successTpl'=> '@CODE:
             	<div class="alert alert-success mt-3">
                 	<h3>Спасибо!</h3>
                 	<p>Мы с вами свяжемся в ближайшее время.</p>
             	</div>',
      'subject' => 'Новая заявка - Стать нашим партнером!',
      'formTpl' => 'partnerForm',
      'reportTpl' => 'partnetReport',
      'fromName' =>'[(site_name)]',
      'captcha'=> 'reCaptcha',
      'captchaParams'=> '{
        "siteKey":"6LfuuqYUAAAAAIP70pXK10MRXTNBjY3tWeKZOsl_",
        "secretKey":"6LfuuqYUAAAAAA8DrYCmea2tmmMbaqq4dL7j1Xlp",
        "errorCodeFailed":"Нажмите галочку \"Я не робот\"",
        "width":200,
   				"height":120,
        "callback":"recaptcha_valid",
        "expired_callback":"recaptcha_exp"
      }',
      'captchaField'=> 'g-recaptcha-response'
    ));
    echo $modx->parseDocumentSource($result);
    die();
    break;


function renderCaptcha(id){
    if($('#'+id+' > *').length === 0){
      grecaptcha.render(id, {
        'sitekey' : $('#'+id).attr('data-sitekey'),
      });
    }
  }
  
  $(document).on("submit",".become-partner .partner-form form",function(e){
    e.preventDefault();
    var m_method=$(this).attr('method');
    var m_action=$(this).attr('action');
    var m_data=$(this).serialize();
    $.ajax({
      type: m_method,
      url: m_action,
      data: m_data,
      resetForm: 'true',
      success: function(result){
        $(".become-partner form").html(result);
        if($(data).('.g-recaptcha').length){
                   renderCaptcha($(data).('.g-recaptcha').attr('id'));
                }
        grecaptcha.render(id, {
          'sitekey' : $('#'+id).attr('data-sitekey')
        });
      }
    });
  });


thank!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question