问题描述
我正在构建一个带有联系表单的网站,我希望能够在同一页面上显示 Google reCAPTCHA 响应(例如“消息发送成功”或“reCAPTCHA 无效。再试一次。”),而不是它转发到我的另一个 .PHP 文件。
我已经通过 localhost 尝试了这个,我能够通过另一个 .PHP 文件获得响应就好了,但是它会让你离开我不想要的主页。
如果我在主网站代码中有联系表单代码,它确实按照我的意愿行事,但我宁愿有两个单独的文件并执行 <form action="assets/PHP/form.PHP" method="post">
。
我已经通过 Paint 尝试了 illustrating this(我不是艺术家)。
我是个新手,大约 5/6 年前我做过一些基本的 HTML/CSS,但从那以后就没有真正编码过。
这是目前的代码:
index.PHP(只是表单)
<form action="assets/PHP/form.PHP" method="post">
<div class="fields">
<div class="field half">
<label for="name">Name</label>
<input type="text" name="name" id="name" required>
</div>
<div class="field half">
<label for="email">Email</label>
<input type="email" name="email" id="email" required>
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5" required></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="My Site Key"></div>
<br/>
<ul class="actions">
<li><input type="submit" name="submit" value="Send Message" class="submit-btn"></li>
</ul>
</form>
form.PHP
<?PHP
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "email@mydomain.co.uk";
$headers = "From: ".$email;
$txt = "You have received an email from ".$name.".\n\n".$message;
$secretKey = "My Secret Key";
$responseKey = $_POST['g-recaptcha-response'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response->success) {
mail($mailTo,$email,$txt,$headers);
header("Location: http://localhost/website/index.PHP?formsent");
echo "Thanks for sending us a message,it was sent successfully. We will get back to you as soon as possible.";
}
else {
echo "Invalid reCAPTCHA. Please try again.";
}
}
?>
希望这是足够的信息,有人可以提供帮助。谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)