简单的php联系表单出现这种错误的原因是什么?
问题描述:
我让我的网站生活,并决定测试我的联系表单,这是我在网上找到的一个简单的php脚本。这似乎很简单,但我无法弄清楚为什么我会死“错误!”当我填写所有字段并点击我的直播网站上的提交。我没有任何使用PHP的经验,所以在这里的任何帮助将非常感激! PHP:简单的php联系表单出现这种错误的原因是什么?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "[email protected]";
$subject = "Customer Contact Message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you for contacting us.";
?>
HTML:
<form class="contact-form" id="contact-form" action="contactform.php" method="post">
<div class="col-md-6">
<input placeholder="Name" name="name" type="text">
</div>
<div class="col-md-6">
<input placeholder="Email" name="email" type="text">
</div>
<div class="col-md-12">
<input placeholder="Subject" name="subject" type="text">
</div>
<div class="col-md-12">
<textarea placeholder="Message" name="message"></textarea>
</div>
<div class="col-md-12">
<button class="btn submit-btn btn-primary" type="submit">Send Message</button>
</div>
</form>
答
<button class="btn submit-btn btn-primary" type="submit">Send
Message</button>
instead use this:
<input type="submit" class="btn submit-btn btn-primary" value="Send Message"
name="submit">
And your php script will be like this:
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Email: $email \n Subject: $subject \n
Message: $message";
$recipient = "[email protected]";
$subject = "Customer Contact Message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or
die("Error!");
echo "Thank you for contacting us.";
}
?>
它只是意味着'mail()'函数返回false,所以你的邮件设置有问题。 – FMashiro
永远不要相信用户输入,验证/清除所有'$ _POST'值。 – Script47
这可以作为一个有趣的小实验来完成,但是你不应该部署这段代码。它很容易被坏人滥用来发送垃圾邮件。 –