【问题标题】:Add Invisible reCAPTCHA in to an existing contact form将 Invisible reCAPTCHA 添加到现有联系表单中
【发布时间】:2017-04-26 07:25:25
【问题描述】:

美好的一天!

要添加安全功能,我想将 Invisible reCAPTCHA 添加到我现有的表单中,在本例中是联系表单。

请看现有代码:

<script>
var blogId = '12345678901';
var contactFormMessageSendingMsg ='Sending...';
var contactFormMessageSentMsg = 'Your message has been sent.';
var contactFormMessageNotSentMsg = 'Message could not be sent. Please try again later.';
var contactFormEmptyMessageMsg ='Message field cannot be empty.';
var contactFormInvalidEmailMsg = 'A valid email is required.'

var widgetLoaded=false;
function sendEmailMsg() {
if(widgetLoaded== false) {
_WidgetManager._RegisterWidget('_ContactFormView', new _WidgetInfo('ContactForm1', 'sidebar', null, document.getElementById('ContactForm1'), {'contactFormMessageSendingMsg': contactFormMessageSendingMsg , 'contactFormMessageSentMsg': contactFormMessageSentMsg , 'contactFormMessageNotSentMsg': contactFormMessageNotSentMsg , 'contactFormInvalidEmailMsg': contactFormInvalidEmailMsg , 'contactFormEmptyMessageMsg': contactFormEmptyMessageMsg , 'title': 'Contact Form', 'blogId': blogId, 'contactFormNameMsg': 'Name', 'contactFormEmailMsg': 'Email', 'contactFormMessageMsg': 'Message', 'contactFormSendMsg': 'Send', 'submitUrl': 'https://www.blogger.com/contact-form.do'}, 'displayModeFull'));
widgetLoaded=true;
document.getElementById('ContactForm1_contact-form-submit').click();
}
return true;
}
</script>
<form name='contact-form'>
<div>Your Name : </div>
<input class='contact-form-name' id='ContactForm1_contact-form-name' name='name' size='30' type='text' value=''/>
<div>Your Email: <em>(required)</em></div>
<input class='contact-form-email' id='ContactForm1_contact-form-email' name='email' size='30' type='text' value=''/>
<div>Your Message: <em>(required)</em></div>
<textarea class='contact-form-email-message' id='ContactForm1_contact-form-email-message' name='email-message' rows='5'></textarea>
<p></p>

<input class='contact-form-button contact-form-button-submit' id='ContactForm1_contact-form-submit' type='button' value='Send' onclick="sendEmailMsg()"/>


<div style='text-align: center; max-width: 450px; width: 100%'>
<p class='contact-form-error-message' id='ContactForm1_contact-form-error-message'></p>
<p class='contact-form-success-message' id='ContactForm1_contact-form-success-message'></p>
</div>
</form>

我尝试从 googlw re captcha 网站插入代码,但它不起作用:

&lt;button class="g-recaptcha" data-sitekey="my site key" data-callback='sendEmailMsg()'&gt;Submit&lt;/button&gt;

是只需要在回调中指明函数还是需要配置class和id??

请帮忙!

【问题讨论】:

    标签: javascript html recaptcha


    【解决方案1】:

    您不应直接致电sendEmailMsg()。如Google documentation 所述,您必须在回调中调用您的函数:

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
      function onSubmit(token) {
        sendEmailMsg();
      }
    </script>
    

    调用 onSubmit 函数,移除您的点击事件,并将 Google 所需的类和数据放入您现有的提交按钮中:

    <input class='g-recaptcha contact-form-button contact-form-button-submit' id='ContactForm1_contact-form-submit' type='button' value='Send' data-sitekey='your site key' data-callback='onSubmit()'/>
    

    这是你的完整代码:

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
      function onSubmit(token) {
        sendEmailMsg();
      }
    </script>
    <script>
      var blogId = '12345678901';
      var contactFormMessageSendingMsg = 'Sending...';
      var contactFormMessageSentMsg = 'Your message has been sent.';
      var contactFormMessageNotSentMsg = 'Message could not be sent. Please try again later.';
      var contactFormEmptyMessageMsg = 'Message field cannot be empty.';
      var contactFormInvalidEmailMsg = 'A valid email is required.'
    
      var widgetLoaded = false;
    
      function sendEmailMsg() {
        if (widgetLoaded == false) {
          _WidgetManager._RegisterWidget('_ContactFormView', new _WidgetInfo('ContactForm1', 'sidebar', null, document.getElementById('ContactForm1'), {
            'contactFormMessageSendingMsg': contactFormMessageSendingMsg,
            'contactFormMessageSentMsg': contactFormMessageSentMsg,
            'contactFormMessageNotSentMsg': contactFormMessageNotSentMsg,
            'contactFormInvalidEmailMsg': contactFormInvalidEmailMsg,
            'contactFormEmptyMessageMsg': contactFormEmptyMessageMsg,
            'title': 'Contact Form',
            'blogId': blogId,
            'contactFormNameMsg': 'Name',
            'contactFormEmailMsg': 'Email',
            'contactFormMessageMsg': 'Message',
            'contactFormSendMsg': 'Send',
            'submitUrl': 'https://www.blogger.com/contact-form.do'
          }, 'displayModeFull'));
          widgetLoaded = true;
          document.getElementById('ContactForm1_contact-form-submit').click();
        }
        return true;
      }
    </script>
    <form name='contact-form'>
        <div>Your Name :</div>
        <input class='contact-form-name' id='ContactForm1_contact-form-name' name='name' size='30' type='text' value='' />
        <div>Your Email: <em>(required)</em></div>
        <input class='contact-form-email' id='ContactForm1_contact-form-email' name='email' size='30' type='text' value='' />
        <div>Your Message: <em>(required)</em></div>
        <textarea class='contact-form-email-message' id='ContactForm1_contact-form-email-message' name='email-message' rows='5'></textarea>
        <p></p>
    
        <input class='g-recaptcha contact-form-button contact-form-button-submit' id='ContactForm1_contact-form-submit' type='button' value='Send' data-sitekey='your site key' data-callback='onSubmit()'/>
    
    
        <div style='text-align: center; max-width: 450px; width: 100%'>
            <p class='contact-form-error-message' id='ContactForm1_contact-form-error-message'></p>
            <p class='contact-form-success-message' id='ContactForm1_contact-form-success-message'></p>
        </div>
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多