我已将一个php文件附加到我的网站,以便用户可以向我发送查询邮件及其详细信息,它将所有数据发送到我的邮件,但在发送邮件后加载时显示内部服务器错误。 这是我的代码:
此表单位于网站的每个页面上
<form style="line-height:15px" name="contactform" action="send_form_email.php" method="post">
<input type="text" placeholder="Full Name" maxlength="50" class="txt" name="name" required><br><br>
<input type="text" placeholder="E Mail" class="txt" maxlength="80" name="email" required><br><br>
<textarea onkeyup="adjust_textarea(this)" placeholder="Message" class="txt" maxlength="1000" name="message" required></textarea><br><br>
<input type="text" placeholder="Phone No." class="txt" maxlength="30" name="phone" required><br><br><br>
<input type="submit" value="Send" class="btn">
</form>
send_form_email.php
<?php
$errors = '';
$myemail = 'legaladvisory@shagunmarriage.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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 = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message\n $phone";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
alert('Thankyou for contacting us');
header('Location: index.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body style="window.location='index.php'">
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
答案 0 :(得分:0)
在设置位置标题之前,您的错误很可能来自底部附近的提醒功能。看起来你似乎正在尝试使用javascript风格的警报,除非你在某个地方有一些名为&#39; alert()&#39;您尚未包含在代码示例中。值得一提的是,在任何时候调用标题时,必须在输出任何输出之前执行此操作,否则您将出错。
答案 1 :(得分:0)
您的代码中没有定义“alert()”函数,它不是我所知道的内置PHP函数。我怀疑评论该线路将解决它。
//alert('Thankyou for contacting us');
即使定义了alert(),也会立即进行重定向。除非您的服务器配置为缓冲输出,否则您不能在使用header()之前将输出发送到浏览器。如果不是这样,那么查看.htaccess文件可能有所帮助。