php 使用AJAX将google recaptcha添加到我们的表单中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用AJAX将google recaptcha添加到我们的表单中相关的知识,希望对你有一定的参考价值。
// add JS for homepage contact us form
function homepage_form_js(){
if ( is_front_page() ){
echo "<script language='javascript' type='text/javascript'>
function ap_validate_is_multi_answered(multiSelectId) {
if(typeof document.getElementById(multiSelectId) != 'undefined') {
var selectObj = document.getElementById(multiSelectId+'[]');
if((selectObj != null) && (selectObj.tagName == 'SELECT' || selectObj.tagName == 'select') && (selectObj.value.length > 0)) {
return true;
}
var inputs = document.getElementsByTagName('input');
var len = inputs.length;
for(var i=0;i<len;i++) {
if((inputs[i].id.indexOf(multiSelectId) != -1) && (inputs[i].selected == true || inputs[i].checked == true)) {
return true;
}
}
}
return false;
}
function ap_validate_9013() {
if(typeof document.getElementById == 'undefined') {
document.ap_form_9013.submit();
return;
}
var form = document.ap_form_9013;
if (form.name === \"\"){
alert( \"There was an error on this form.\" );
} else if (((typeof form.ap_field_67304 == 'undefined') || (typeof form.ap_field_67304.value == 'undefined')) && !ap_validate_is_multi_answered('ap_field_67304')) {
alert( 'Please enter all required fields: First Name');
} else if ((typeof form.ap_field_67304 != 'undefined') && (typeof form.ap_field_67304.value != 'undefined') && (form.ap_field_67304.value === \"\")) {
alert( \"Please enter all required fields: First Name \" );
} else if (((typeof form.ap_field_67302 == 'undefined') || (typeof form.ap_field_67302.value == 'undefined')) && !ap_validate_is_multi_answered('ap_field_67302')) {
alert( 'Please enter all required fields: Email Address');
} else if ((typeof form.ap_field_67302 != 'undefined') && (typeof form.ap_field_67302.value != 'undefined') && (form.ap_field_67302.value === \"\")) {
alert( \"Please enter all required fields: Email Address \" );
} else {
var captcha = grecaptcha.getResponse();
jQuery.ajax({
url:'/wp-admin/admin-ajax.php',
method:'POST',
data: {action: \"send_captcha\", response:captcha}
}).done(function(data, textStatus, jqXHR){
data = jQuery.parseJSON(data);
console.log(data);
if ( data.success === true ) {
document.ap_form_9013.submit();
}
});
}
}
</script>";
}
}
add_action('wp_footer', 'homepage_form_js');
function captchaAjax(){
$ch = curl_init();
$response = $_POST['response'];
$secret = '6LfCMBUTAAAAABXlZCAqe3fECaNjad542WWMsavF';
$postData = new stdClass();
$postData->response = $response;
$postData->secret = $secret;
$url = "https://www.google.com/recaptcha/api/siteverify?" . http_build_query($postData);
// set url
curl_setopt($ch, CURLOPT_URL, $url );
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/* curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); */
curl_setopt($ch, CURLOPT_HEADER, 0);
/* curl_setopt($ch, CURLOPT_POST, 1); */
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// echo back response
echo $output;
wp_die();
}
add_action('wp_ajax_send_captcha','captchaAjax');
add_action( 'wp_ajax_nopriv_send_captcha', 'captchaAjax' );
以上是关于php 使用AJAX将google recaptcha添加到我们的表单中的主要内容,如果未能解决你的问题,请参考以下文章
PHP CakePHP的Google Ajax Libraries API处理程序
PHP 从Wordpress中的Google AJAX Libraries API加载jQuery