当我点击提交按钮时,即使我确实设置了PHP,也没有任何反应。我一开始以为我的代码显然是错误的。但现在我甚至从这里复制/粘贴代码,检查是正确的,但没有任何反应。
以下是代码:
<?php
if(isset($_POST['submit'])){
$to = "zezesurla@gmail.com"; // 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>
这可能是服务器中的问题而不是实际代码吗?因为我尝试了在各种教程中找到的其他几个例子,并且出现了同样的问题。
或者我还在做错什么?
答案 0 :(得分: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">
这些更改不会解决表单容易发送垃圾邮件的问题,但它会解决这些问题:
mail()
来电错误,则会记录该错误,并会显示一条消息(也会确认所涉及的电子邮件地址)"\n"
),否则应该使用它。<head>
部分之前将文本写入浏览器来生成错误的HTML action
来处理表格