我创建了一个联系表单,它确实有效,但现在我想在“发送”按钮下方提交表单后显示“已发送消息”。
HTML文件
<form action="send_mail.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Name</label>
<input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="email2">Email address</label>
<input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="message2">Message</label>
<textarea class="form-control" id="message2" name="message2" placeholder="Message" required="" rows="8"></textarea>
</div>
<div class="6u 12u$(small)">
<input id="copy" name="copy" type="checkbox" />
<label for="copy">Email me a copy of this message</label>
</div>
<input class="btn btn-default" type="submit" value="Send" />
</form>
PHP代码(不同的文件)
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello@domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
答案 0 :(得分:0)
非常简单,使用带有if/else
的条件语句。
if( mail($recipient, $subject, $formcontent, $mailheader) ){
echo "Sent";
} else {
echo "Error, check your logs";
}
另外,如果您的整个代码都在同一个文件中,那么最好对所有输入/ POST数组使用条件语句并检查empty()
字段。
答案 1 :(得分:0)
正如Funk Forty Niner所说:
使用if / else语句:
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello@domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)) {
die("Error!");
} else {
echo "Success!";
}
?>
if/else
文档:http://php.net/manual/en/control-structures.elseif.php
答案 2 :(得分:0)
您可以在 mail()函数
之后使用诸如success = true之类的参数将您的php重定向到您的htmlmail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: index.php?success=true');
将.html重命名为.php
并在
下的html(现在的php)中<input class="btn btn-default" type="submit" value="Send" />
<?php
if ( isset($_GET['success']) && $_GET['success']==true )
echo "Message Sent";
?>