【发布时间】:2016-01-10 21:03:16
【问题描述】:
我有一个内置在 index.html 中的 html 联系表单,然后我有一个 mail.php 文件,它可以发送邮件并使用一些 Javascript。当我填写表单并提交时,我已经对其进行了编码以发送邮件,然后它会弹出一个成功消息框,然后重定向回 index.html。
我想要的是将表单替换为成功消息,以避免页面刷新并避免弹出消息框。
来自 index.html 的表单:
<form action="mail.php" method="POST">
<div class="row uniform">
<div class="6u 12u$(xsmall)">
<input type="text" name="name" id="name" value="" placeholder="Name" />
</div>
<div class="6u$ 12u$(xsmall)">
<input type="email" name="email" id="email" value="" placeholder="Email" />
</div>
<div class="12u$">
<!--<div class="select-wrapper">
</div>-->
</div>
<div class="12u$">
<textarea name="message" id="message" placeholder="Enter your message" rows="6"></textarea>
</div>
<center><div class="g-recaptcha" data-sitekey=""></div></center>
<div class="12u$">
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>
php 文件、电子邮件地址和 recaptcha 令牌因明显原因而被删除:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name \n email: $email \n Message: $message";
$recipient = 'email address here';
$subject = 'Message from Website';
$mailheader = "From: $email \r\n";
$captcha;
{
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha)
{
echo '<script language="javascript">';
echo 'alert("Please check the Captcha")';
echo '</script>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>Please do not try and spam here.</h2>';
}else
{
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo '<script language="javascript">';
echo 'alert("Your Message successfully sent, we will get back to you ASAP.");';
echo 'window.location.href="index.html";';
echo '</script>';
}
} ?>
这是我实际看过的一个话题:
【问题讨论】:
标签: javascript php html forms