我正在使用Codeigniter和Ajax验证并提交电子邮件表单。 我尝试了许多方法,它始终为我提供结果{“ sent”:false},在添加Recaptcha行之前,它的效果很好。
这是我的代码:
custom.script.js
$("#ajax-contact-form").submit(function() {
var href = 'http://localhost:8888/en/send';
var name = $("#form-name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var title = $("#form-title").val();
var message = $("#form-message").val();
$.ajax({
type: "POST",
url: href ,
dataType: "json",
data: {
name: name,
email: email,
phone: phone,
title: title,
message: message,
//UPDATE from recaptcha without quotes to 'g-recaptcha-response'
'g-recaptcha-response': grecaptcha.getResponse()
},
success: function(msg) {
if(msg.sent) {
console.log("Successssssssssss");
$('#result').addClass('success');
$('#result').html('Email was sent successfully!');
} else {
$('#result').addClass('error');
$('#result').html('Email was NOT sent!');
}
}
});
return false;
});
En.php
function send()
{
$this->load->helper('form');
$this->load->library('form_validation');
$google_url="https://www.google.com/recaptcha/api/siteverify";
//UPDATE from recaptcha to 'g-recaptcha-response'
$str = $this->input->post('g-recaptcha-response');
$secret = 'GOOGLE_SECRET_KEY';
$url=$google_url."?secret=".$secret."&response=".$str;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$status= json_decode($output, true);
if ($status['success']) {
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = 'ANY TEXT';
$emailto = "email@domain.com";
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if($this->email->send()){
echo json_encode(array("sent"=>TRUE));
}
}
else{
echo json_encode(array("sent"=>FALSE));
}}
google密钥是正确的,我不知道为什么它没有成为现实 if语句。
答案 0 :(得分:0)
您好,我们来讨论一下。似乎返回的数据 $ status ['success'] 需要使用“ ==”运算符与所需值进行比较。您可以像这样检查它。
if ($status['success'] == true) {
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = 'ANY TEXT';
$emailto = "email@domain.com";
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if($this->email->send()){
echo json_encode(array("sent"=>TRUE));
}
}
也请在将其置于条件(如果为else语句)之前区分这些值。
答案 1 :(得分:0)
最终成功了,这是最终代码:
function send()
{
$this->load->helper('form');
$this->load->library('form_validation');
$google_url="https://www.google.com/recaptcha/api/siteverify";
$str = $this->input->post('g-recaptcha-response');
$secret = 'GOOGLE SECRET KEY';
$url=$google_url."?secret=".$secret."&response=".$str;
$response = json_decode(file_get_contents($url), true);
if ($response["success"] != false) {
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = 'ANY TEXT';
$emailto = "email@domain.com";
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if($this->email->send()){
echo json_encode(array("sent"=>TRUE));
}
}
else{
echo json_encode(array("sent"=>FALSE));
}
}
答案 2 :(得分:0)
如果您使用'g-recaptcha-response'并且$ status ['success']在给定的实现中返回null,则最好使用如下所示的cURL request方法:
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret_key = 'YOUR_SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$message = 'Google reCaptcha Test';
if(!empty($response))
{
$url = $google_url."?secret=".$secret_key."&response=".$response;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$curlData = curl_exec($curl);
curl_close($curl);
$res = json_decode($curlData, TRUE);
if($res['success'] == 'true')
$message = "Success!";
else
$message = "Enter captcha again!";
}