gpt4 book ai didi

javascript - 将隐形 reCAPTCHA 添加到现有联系表单中

转载 作者:行者123 更新时间:2023-12-03 04:27:42 24 4
gpt4 key购买 nike

美好的一天!

为了添加安全功能,我想将隐形 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 站点插入代码,但它不起作用:

<button class="g-recaptcha" data-sitekey="my site key" data-callback='sendEmailMsg()'>Submit</button>

我需要只在回调中指示函数还是需要配置类和id?

请帮忙!

最佳答案

您不应直接调用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>

关于javascript - 将隐形 reCAPTCHA 添加到现有联系表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com