我正在尝试确保在发送包含我的表单数据的电子邮件表单之前填充了google recaptcha2。然而,由于我的网站需要基于javascript的电子商务功能,表单数据首先传递给javascript文件,然后作为另一种形式发布到php文件。我试图找到一种方法来采取g-recaptcha-response并从原始表单到javascript表单到php sendmail文件一直到它,以验证它是否以正常的后端方式填充。
首先是剥离的HTML,所以你可以看到我把所有东西放在正确的位置,网站密钥由'MySiteKey'补充:
<head><script src='https://www.google.com/recaptcha/api.js'></script></head>
<form name="form" id="form">
<fieldset>
<ol>
<li><label for="emaile">Email Address: </label>
<input type="email" placeholder=" Email Address" name="emaile" class="input" required id="emaile" /></li>
</ol>
<div class="g-recaptcha" data-sitekey="MySiteKey"></div>
<button class="submit" type="submit" name="Submit" style="cursor:pointer" formaction="javascript:simpleCart.checkout()">Submit</button>
</fieldset>
</form>
接下来是外部js文件的大规模简化但相关的部分
simpleCart.checkout()
emaile = document.form.emaile.value;
var b = document.createElement("form");
b.style.display = "none";
b.method = "POST";
b.action = "/php/sendjs.php";
b.acceptCharset = "utf-8";
b.appendChild(a.createHiddenElement("emaile", emaile));
b.appendChild(a.createHiddenElement("g-recaptcha-response", g-recaptcha-response));
document.body.appendChild(b);
b.submit();
document.body.removeChild(b)
我想附加'g-recaptcha-response'字符串以传递给单独的php文件和其他表单数据,以检查在发送之前是否填充了验证码:
<?php
$captcha = $_POST['g-recaptcha-response'];
$secretKey = "MySecretSiteKey ";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify? secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>NOT SENT</h2>';
} else {
$subject = 'Order Inquiry from DIY Soakwells';
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: order@DIYSoakwells.com' . "\r\n" .
'Reply-To: contact@diysoakwells.com.au' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$emaile = $_POST['emaile'];
$to = "diysoakwells@hotmail.com,$emaile";
$text = "<html><body><p><b>Customers Email Address:</b> $emaile</p>
</html>
</body>";
$body = $text;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
}
?>
除了检查重新填充之外,一切都有效,我明白我在这里的工作方式不会像现在这样,但我试图说明我的方法而且我已经尝试了几个小时让它工作到没有有用,任何帮助将不胜感激。希望有人能够理解我想要做的事,谢谢。
中的键/值对我非常愿意接受其他建议。
答案 0 :(得分:1)
这应解决所有问题。
function createHiddenElement(name, value) {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", name);
input.setAttribute("value", value);
return input;
}
simpleCart.checkout()
var b = document.createElement("form");
b.style.display = "none";
b.method = "POST";
b.action = "/php/sendjs.php";
b.acceptCharset = "utf-8";
b.appendChild(createHiddenElement('emails', document.form.emaile.value));
b.appendChild(createHiddenElement('g-recaptcha-response', document.getElementById('g-recaptcha-response').value));
document.body.appendChild(b);
b.submit();
document.body.removeChild(b)