2017-05-30 67 views
0

当我点击提交按钮时,即使我设置了PHP,也没有任何反应。起初我以为我的代码显然是错误的。但现在我甚至从这里复制/粘贴代码,这被检查为正确的,但没有任何反应。当我点击我的联系表单中的提交按钮时,没有任何反应

这里是代码:

<?php 
if(isset($_POST['submit'])){ 
    $to = "[email protected]"; // this is your Email address 
    $from = $_POST['email']; // this is the sender's Email address 
    $first_name = $_POST['first_name']; 
    $last_name = $_POST['last_name']; 
    $subject = "Form submission"; 
    $subject2 = "Copy of your form submission"; 
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; 
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; 

    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly."; 
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
} 
?> 

<!DOCTYPE html> 
<head> 
<title>Form submission</title> 
</head> 
<body> 

<form action="" method="post"> 
First Name: <input type="text" name="first_name"><br> 
Last Name: <input type="text" name="last_name"><br> 
Email: <input type="text" name="email"><br> 
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 

</body> 
</html> 


</div> 

这会不会是服务器中的问题,而不是实际的代码?因为我尝试了其他几个我在各种教程中找到的例子,并且发生了同样的问题。

还是我还在做错什么?

+1

回显是否打印“邮件发送”? –

+1

请你可以在你的代码块开头删除''。 – manian

+0

它是否显示任何类型的错误,如“警告:邮件():无法连接到邮件服务器”。或者当您提交时打印某些内容? – manian

回答

0

至少,我会做如下修改代码:

<?php 
$msg=''; 
if (isset($_POST['submit'])) 
{ 

...

if (!mail($to,$subject,$message,$headers)) 
     error_log(($msg='Send form submission to "'.$to.'" failed')); 
    else if (!mail($from,$subject2,$message2,$headers2)) // sends a copy of the message to the sender 
     error_log(($msg='Send copy of form submission to user "'.$from.'" failed')); 
    else $msg='Mail Sent. Thank you ' . $first_name . ', we will contact you shortly.'; 

...

<body> 
<?php if ($msg) 
{ ?> 
<p><?php echo $msg ?></p> 
<?php 
} ?> 
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 

这些改变将不会解决公关形式的oblem易受发送垃圾邮件,但它会解决这些问题:如果发生mail()呼叫错误

  • ,它会被记录下来,并会显示一条消息(这也将确认所涉及的邮件地址)
  • 单引号字符串不必由PHP解释器进行解析,除非你是嵌入需要
  • 你会不会产生被解释变量或字符序列(如"\n")应使用在文档的<head>部分
  • 之前将文本写入浏览器210
  • 您可以放心正确使用action来处理表格
相关问题