提问人:BlueDogRanch 提问时间:11/12/2023 最后编辑:BlueDogRanch 更新时间:11/20/2023 访问量:119
如何在 Getform 和 Ajax 表单提交中使用 hCaptcha 回调?
How can I use an hCaptcha callback with Getform and Ajax form submission?
问:
我正在将 Getform https://getform.io/ 用于 Javascript 联系表单,以及 hCaptcha hcaptcha.com。GetForm 完全支持 hCaptcha https://docs.getform.io/features/spam-filtering/hcaptcha/。但是 Getform 和 hCaptcha 都没有提供代码级别的支持,因此,我在这里问。
我正在使用 Getform Ajax 表单,该表单在提交表单后使用户保持在同一页面上。它非常基本,来自 Getform 的剪切和粘贴示例。我为每个字段使用“必需”属性,Getform 会自动验证这些属性,并且在所有字段验证之前不允许表单按钮提交单击。表单和必填字段有效,表单在不集成 hCaptcha 时提交。
但我想做的是集成 hCaptcha,自动将 hCaptcha 质询绑定到提交按钮,所以 hCaptcha 只有在点击提交按钮后才会出现。
为此,我需要一个回调,如 https://docs.hcaptcha.com/invisible#programmatically-invoke-the-challenge 所述,回调基本上是这样的:
function onSubmit(token) {
document.getElementById('ajaxForm').submit();
}
但是我添加的回调不适用于 Ajax 函数。使用下面的代码,单击表单提交按钮时会出现 hCaptcha,解决后,表单将提交。Getform 日志显示 hCaptcha 已解决,并在 Getform 中收到消息,但由于表单字段验证不会在表单提交之前进行,因此如果在提交时将必填表单字段留空,则消息中的必填表单字段可能会为空。
问题:我如何在下面的jQuery Ajax函数中使用其中任何一个(或其他函数),所以顺序是这样的:如果没有必需的表单字段,就无法单击提交,当它们被填写并提交时,就会显示hCaptcha,如果hCaptcha成功解决,则表单最终通过提交。function onSubmit(token)
post
HTML格式:
<div class="form-container">
<form id="ajaxForm" accept-charset="UTF-8" action="https://getform.io/f/form-id" method="POST">
<label for="email" required="required">Your Email</label><br>
<input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required="required">
<label for="name">Your Name</label><br>
<input type="text" name="name" class="form-control" id="name" placeholder="Enter name" required="required">
<label for="name">Your Message</label>
<textarea cols="50" rows="5" name="message" class="form-control" id="message" placeholder="Enter Message" required="required" minlength="20" maxlength="500"></textarea>
<button class="h-captcha" data-sitekey="site-key" data-callback="onSubmit" type="submit">Submit</button>
</form>
<div class="success">Message sent OK!</div>
</div>
Javascript的:
jQuery('.success').removeClass('is-active'); // Hide div on page load
jQuery("#ajaxForm").submit(function(e){
e.preventDefault();
var action = jQuery(this).attr("action");
jQuery.ajax({
type: "POST",
url: action,
crossDomain: true,
data: new FormData(this),
dataType: "json",
contentType: "multipart/form-data",
processData: false,
contentType: false,
headers: {
"Accept": "application/json"
}
}).done(function() {
jQuery('.success').addClass('is-active'); // Shows sucess div
jQuery("button[type='submit']").prop('disabled',true); //Disabled button to prevent multiple submits
$("#ajaxForm :input").val(''); //clears form fields after submit
}).fail(function() {
alert('Something broke - you should try again');
});
});
function onSubmit(token) {
document.getElementById('ajaxForm').submit(); // Callback as required by Hcaptcha, which doesn't work
}
答:
更新:
对不起,我的错,关键是改用数据回调,这有效,但你需要修复你的jQuery.ajax调用,因为我不知道新的FormData($(“#ajaxForm”))是否会像那样工作,认为你需要看到通过JQuery AJAX一起发送FormData和字符串数据?
function onLoad() {
var element = document.getElementById("submit");
element.onclick = validate;
}
$("#ajaxForm").submit(function (event) {
event.preventDefault();
const response = grecaptcha.getResponse();
// can add in js vaildation here if you wanted to
if (!document.getElementById("exampleInputName").value) {
alert("You must add text to the required field");
return false;
}
// Check capture has been completed
if (response.length === 0) {
alert("Please complete recaptha");
return false;
}
// could manually tigger capture promt with: hcaptcha.execute();
// instead of the respone length check
});
function onSubmit(token) {
alert("thanks " + document.getElementById("exampleInputName").value);
const action = $("#ajaxForm").attr("action");
//TODO FIX FORM DATA SEE: https://stackoverflow.com/questions/21060247/send-formdata-and-string-data-together-through-jquery-ajax
/*
$.ajax({
type: "POST",
url: action,
crossDomain: true,
data: new FormData($("#ajaxForm")),
dataType: "json",
contentType: "multipart/form-data",
processData: false,
contentType: false,
headers: {
"Accept": "application/json"
}
}).done(function () {
$('.success').addClass('is-active');
}).fail(function () {
alert('An error occurred please try again later.')
})
*/
}
h1 {
font-size: 20px;
margin-top: 24px;
margin-bottom: 24px;
}
img {
height: 60px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://js.hcaptcha.com/1/api.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 offset-md-3 mt-5">
<a href="https://getform.io?ref=codepenHTML">
<img src='https://i.imgur.com/O1cKLCn.png'>
</a>
<br>
<a href="https://getform.io?ref=codepenHTML" class="mt-3 d-flex">Getform.io | Get your free endpoint now</a>
<h1>Getform.io hCaptcha HTML Form Example with File Upload</h1>
<form accept-charset="UTF-8" action="https://getform.io/f/d709b72b-d30b-4316-9cc1-4d1d33778a8b" method="POST" enctype="multipart/form-data" id="ajaxForm">
<div class="form-group">
<label for="exampleInputName">Full Name</label>
<input type="text" name="fullname" class="form-control" id="exampleInputName" placeholder="Enter your name and surname" required="required">
</div>
<div class="form-group">
<label for="exampleInputEmail1" required="required">Email address</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Favourite Platform</label>
<select class="form-control" id="exampleFormControlSelect1" name="platform" required="required">
<option>Github</option>
<option>Gitlab</option>
<option>Bitbucket</option>
</select>
</div>
<hr>
<div class="form-group mt-3">
<label class="mr-2">Upload your CV:</label>
<input type="file" name="file">
</div>
<hr>
<div class="h-captcha" id="hcaptcha" data-sitekey="194e8efb-f32d-4d44-8ffb-b54694c2f321" data-callback="onSubmit"></div>
<button id="submit" class="btn btn-primary">Submit</button>
</form>
</div>
评论