有人帮我弄清楚为什么我的PHP总是吐出google recaptcha没有被设置。它在 echo“recaptcha_empty”时失败了。在此先感谢 忽略google api密钥。
if(isset($ _ POST [“g-recaptcha-response”]))
PHP代码
<?php
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["phone"]) && isset($_POST["message"])) {
if (isset($_POST["g-recaptcha-response"])) {
$username = $_POST["name"];
$useremail = $_POST["email"];
$userphone = $_POST["phone"];
$usermessage = $_POST["message"];
$to = "mail@domain.com";
$subject = "Inquiry form MKM Website";
$message = "From: $username\n E-Mail: $useremail\n Phone: $userphone\n\n\n Message:\n\n $usermessage";
$header = "Generated by MKM Website";
// your secret key
$secretKey = "secret_key";
$captcha = $_POST["g-recaptcha-response"];
$ip = $_SERVER["REMOTE_ADDR"];
$verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$response = json_decode($verify, true);
// if submitted check response
if ($response["success"] != false) {
if (mail($to, $subject, $message, $header)) {
echo "success";
} else {
echo "The server failed to send the message. Please try again later.";
exit;
}
} else {
echo "recaptcha_fail";
exit;
}
} else {
echo "recaptcha_empty";
exit;
}
} else {
echo "Form can not be empty..!!";
exit;
}
JAVAScript代码
function submitForm() {
var reply = getId("reply");
reply.innerHTML = "<h4>Sending message...</h4>";
var name = getId("user-name");
var email = getId("user-email");
var phone = getId("user-phone");
var messa = getId("user-message");
var formdata = new FormData();
formdata.append("name", name.value);
formdata.append("email", email.value);
formdata.append("phone", phone.value);
formdata.append("message", messa.value);
var ajax = new XMLHttpRequest();
ajax.open("POST", "contact.php");
ajax.onreadystatechange = function () {
switch(ajax.readyState) {
case 1:
reply.innerHTML = "<h4>Loading.</h4>";
break;
case 2:
reply.innerHTML = "<h4>Loading..</h4>";
break;
case 3:
reply.innerHTML = "<h4>Loading...</h4>";
break;
}
if (ajax.readyState == 4 && ajax.status == 200) {
if (ajax.responseText === "success") {
getId("reply").innerHTML = '<h4>Thanks ' + name.value + ', your message has been sent.</h4>';
name.value = "";
email.value = "";
phone.value = "";
messa.value = "";
} else if (ajax.responseText === "recaptcha_empty") {
getId("reply").innerHTML = '<h5>Please tick the captcha box.</h5>';
} else if (ajax.responseText === "recaptcha_fail") {
getId("reply").innerHTML = '<h5>Recaptcha fail.. please try again later.</h5>';
}
else {
getId("reply").innerHTML = ajax.responseText;
}
}
}
ajax.send(formdata);
}
function getId(_str) {
return document.getElementById(_str);
}
HTML表格
<form class="form-horizontal" onsubmit="submitForm(); return false">
<div class="form-group">
<label for="user-name" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-name" id="user-name" placeholder="Your name" required>
</div>
</div>
<div class="form-group">
<label for="user-email" class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="user-email" id="user-email" placeholder="Your email address" required>
</div>
</div>
<div class="form-group">
<label for="user-phone" class="col-lg-2 control-label">Phone</label>
<div class="col-lg-10">
<input type="tel" class="form-control" name="user-phone" id="user-phone" placeholder="Your phone number" required>
</div>
</div>
<div class="form-group">
<label for="user-message" class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea name="user-message" id="user-message" class="form-control" cols="20" rows="5" placeholder="Your message - max 200 characters" maxlength="200" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<div class="g-recaptcha" data-sitekey="my_site_key"></div>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<div id="reply" class="text-center"></div>
</form>