所有。我正在努力用PHP创建一个联系表单,但由于某些原因我无法让它工作。消息未通过,并且不显示“谢谢您的消息”响应。我的PHP技能非常新手,所以非常感谢任何帮助!
<div id="form-div">
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
$self = $_SERVER['PHP_SELF'];
?>
<form class="form" id="form1" method="post" name="contactForm" action=" <?php echo $self; ?> ">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
</p>
<p class="text">
<textarea name="comment" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
</div>
</form>
<?php
} else {
$name = $_POST['name'];
$emailFrom = $_POST['email'];
$comment = $_POST['comment'];
$emailTo = "address@email.com";
$subject = "Form for Website";
$header = "From: $name <$emailFrom>\r\nReply-To: $emailFrom\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=iso-8859\r\n";
$message = "<b>From:</b> $name<br><b>Email:</b> $emailFrom<br><b>Message:</b> <br>$comment";
mail($emailTo, $subject, $message, $header);
echo"Thank you for your message.";
}
?>
</div>
答案 0 :(得分:1)
通常我会建议发送电子邮件的代码在任何html输出之前 - 尽管我不是100%确定它会遭遇在其他地方经常看到的可怕的headers already sent
错误。您可能也希望考虑过滤POSTed变量并使用strip_tags
。然后根据调用mail
....
<?php
session_start();
$res=false;
/* The `PHP_SELF` server var is not considered secure */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['email'],$_POST['name'] ) ){
@ob_clean();
$name = strip_tags( filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING ) );
$emailFrom = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ), FILTER_VALIDATE_EMAIL );
$comment = strip_tags( filter_input( INPUT_POST, 'comment', FILTER_SANITIZE_STRING ) );
$emailTo = "address@email.com";
$subject = "Form for Website";
$header = "From: $name <$emailFrom>\r\nReply-To: $emailFrom\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type:text/html;charset=iso-8859\r\n";
$message = "<b>From:</b> $name<br><b>Email:</b> $emailFrom<br><b>Message:</b> <br>$comment";
/* You do not want any HTML output before trying to send mail */
$res=@mail( $emailTo, $subject, $message, $header );
}
?>
<html>
<head>
<title>Contact form</title>
</head>
<body><!-- I don't understand the syntax used in the class attributes of these form elements... -->
<div id="form-div">
<form class="form" id="form1" method="post" name="contactForm"><!-- omit the `action` tag to send, by default, to the same page -->
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
</p>
<p class="text">
<textarea name="comment" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
</div>
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['email'],$_POST['name'] ) ){
echo $res ? 'Thankyou for your mail' : 'Sorry, there was a problem';
}
?>
</div>
</body>
</html>
答案 1 :(得分:0)
检查服务器中是否关闭了php错误。在localhost中,如果未正确配置,则发送邮件将失败。在这种情况下,进程退出邮件发送代码,即mail($emailTo, $subject, $message, $header);
,您的感谢信息将不会执行。检查一下。
答案 2 :(得分:0)
如何替换
if($_SERVER['REQUEST_METHOD'] != 'POST') {
到
if(count($_POST) < 1) {