我正在尝试创建一个发送电子邮件的联系表单。我在主页上有一个简单的版本,可以工作并重定向到感谢页面。当我尝试在联系页面上运行相同的 PHP 脚本时,它会发送表单但不会重定向到感谢页面。
发送电子邮件和重定向的 index.html:
<section class="appointment-one" id="booking">
<div class="container wow fadeInUp" data-wow-duration="1.5s">
<div class="inner-container">
<h3 class="appointment-one__title" >Book a meeting</h3><!-- /.appointment-one__title -->
<p class="appointment-one__text">Book a free consultation with one of our experts...</p>
<!-- /.appointment-one__text -->
<form action="sendmail.php" method="POST" class="appointment-one__form row">
<div class="col-lg-4">
<input type="text" class="miyami_input" placeholder="Name" name="name" required>
</div><!-- /.col-lg-4 -->
<div class="col-lg-4">
<input type="email" class="miyami_input" placeholder="Email" name="email" required>
</div><!-- /.col-lg-4 -->
<div class="col-lg-4">
<select name="discussion" class="selectpicker">
<option value="">Discussion about</option>
<option value="General">General</option>
<option value="IT Solutions">IT Solutions</option>
<option value="IT Staffing">IT Staffing</option>
<option value="IT Outsourcing">IT Outsourcing</option>
</select>
</div><!-- /.col-lg-4 -->
<div class="col-lg-12">
<div class="appointment-one__bottom">
<div class="form_button">
<button value="Book a meeting" class="miyami_btn g_bg" type="submit">Book a meeting</button>
</div>
</div><!-- /.appointment-one__bottom -->
</div><!-- /.col-lg-12 -->
</form><!-- /.appointment-one__form -->
</div><!-- /.inner-container -->
</div><!-- /.container -->
</section><!-- /.appointment-one -->
sendmail.php 文件
<?php
$errors = '';
$myemail = 'info@vitosolutions.co.za';//<-----Put Your email address here.
if(empty($_POST['name']) || empty($_POST['email']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$subjects = $_POST['subjects'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$discussion = $_POST['discussion'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Email from VITO";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name".
"\n Discussion: $discussion".
"\n subject: $subjects \n ".
"Email: $email_address\n Message: $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
$mail_status = mail($to,$email_subject,$email_body,$headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
// alert('Thank you for the message. We will contact you shortly.');
window.location.href = 'thankyou.html';
</script>
<?php
}else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info@vitosolutions.co.za');
window.location.href = 'contact.html';
</script>
<?php }
//redirect to the 'thank you' page
header('Location: thankyou.html');
}
?>
但是当我在联系页面上提交表单时,它不会重定向。
不重定向的contact.html页面
<div class="contact-info-one__content">
<div class="container contact-one">
<div class="block-title text-center">
<h2 class="block-title__title text-white">Get in touch</h2><!-- /.block-title__title -->
<p class="block-title__text">Let us help you with your IT requirements.
</p><!-- /.block-title__text -->
</div><!-- /.block-title -->
<form action="sendmail.php" method="POST" class="contact-form-vaidated contact-one__form row">
<div class="col-lg-6">
<input type="text" class="miyami_input" placeholder="Name" name="name" required>
</div><!-- /.col-lg-6 -->
<div class="col-lg-6">
<input type="email" class="miyami_input" placeholder="Email" name="email" required>
</div><!-- /.col-lg-6 -->
<div class="col-lg-12">
<input type="text" class="miyami_input" placeholder="Subject" name="subjects" required>
</div><!-- /.col-lg-6 -->
<div class="col-lg-12">
<textarea class="miyami_textarea" placeholder="Message" name="message"></textarea>
</div><!-- /.col-lg-12 -->
<div class="col-lg-12 text-center">
<div class="form_button">
<button value="Send" class="thm-btn" type="submit">Send</button>
</div>
</div><!-- /.col-lg-12 -->
</form>
</div><!-- /.container -->
</div><!-- /.contact-info-one__content -->
答案 0 :(得分:0)
<?php
$errors = '';
$myemail = 'info@vitosolutions.co.za';//<-----Put Your email address here.
if(empty($_POST['name']) || empty($_POST['email']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$subjects = $_POST['subjects'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$discussion = $_POST['discussion'];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email_address)) {
$errors .= "\n Error: Invalid email address";
}
if(empty($errors)) {
$to = $myemail;
$email_subject = "Email from VITO";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name".
"\n Discussion: $discussion".
"\n subject: $subjects \n ".
"Email: $email_address\n Message: $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
$mail_status = mail($to,$email_subject,$email_body,$headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
// alert('Thank you for the message. We will contact you shortly.');
window.location.href = 'thankyou.html';
</script>
<?php
}else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info@vitosolutions.co.za');
window.location.href = 'contact.html';
</script>
<?php }
}
else {
?>
<script language="javascript" type="text/javascript">
alert('Errors: <?php echo $errors;?>');
</script>
<?php
}
?>
答案 1 :(得分:-2)
我总是在位置标头中包含重定向状态代码。我没有试过没有它,但我知道它确实适用。
// status depends on http protocol
if(!$status) {
$status = strpos($_SERVER['SERVER_PROTOCOL'],'1.1') ? 303 : 302;
}
// set redirect headers
http_response_code($status);
header("Location: $to");
在重定向之前发送的任何输出都将防止导致 header()
失败,因此您可能需要先检查 headers_sent()
。这一直是我过去重定向问题的原因。