我的网站上有一个联系表,其中包含来自freecontactform.com的代码,该代码提供了以下PHP代码。我在脚本的底部添加了自己的HTML,以在提交表单后创建一个醒目页面,但是我希望在同一页面上显示一条警告,例如“邮件已发送”,没有重定向。有人可以告诉我正确的代码以实现我正在寻找的同一页警报吗?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ts95studios@gmail.com";
$email_subject = "RE: TS95Studios.com";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['subject']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="http://ts95studios.com/css/TS95StudiosMainCSS.css" rel="stylesheet" type="text/css">
<link href="http://ts95studios.com/css/codepen.css" rel="stylesheet" type="text/css">
<body class="success">
<div class="success">
<div class="successinner">
<img src="http://ts95studios.com/images/TS95Logo.png" class="ts95logo">
<h1 class="author fontwhite">Message Sent!</h1>
<p class="pageintro fontwhite">Thank you for reaching out, I will be in touch soon.</p>
<a href="http://ts95studios.com" class="contactmelinkb"><button class="actionbutton">BACK TO SITE</button></a>
</div>
</div>
</body>
<?php
}
?>
答案 0 :(得分:0)
您确实有两个选择。
对于选项1,您可以执行以下操作:
<?php
$email_sent = false;
if(isset($_POST['email'])) {
## Process the email here
$email_sent = true;
}
?>
<form action="{{THIS SAME FILE}}" method="post">
...
</form>
<? if($email_sent) { ?>
Thanks for contacting me/us/whoever!
<? } ?>
对于选项2,来自W3Schools的AJAX和PHP的here is an example。本质上,使用Javascript,您将调用电子邮件发送脚本,从表单中提供所有信息,并接收脚本输出的任何内容。