我是php的新手,无法弄清楚这有什么问题。任何帮助,将不胜感激。此外,有一种方法可以测试它是我还是我的主机服务不允许我发送电子邮件。所以,如果我有办法,我可以告诉我。
<?php
session_start();
$visitor_email = '';
$user_message = '';
//Validates the email address that the user entered
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
//When user clicks 'submit'
if (!isset($_POST['submit']))
{
$visitor_email = $_POST['email'];
$user_message = $_POST['message'];
//if user entered email address
if (isset($_POST['email']))
{
// Check if email address is valid
$mailcheck = spamcheck($visitor_email);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$to = "example@yahoo.com";
$subject="Customer Feedback";
$from = $visitor_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A patron submitted the contact form:\n".
"Email: $visitor_email \n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
header('Location: home.html');
}
}
}
?>
<form id="feedBackForm" action="<?php echo htmlentities($_SERVER['PHP_SELF'])?>" method="post">
<fieldset id="personal">
<legend>Your Information</legend>
<label for="email">Contact Email: </label>
<input id="email" name="email" type="email" value='<?php echo htmlentities($visitor_email) ?>'/>
</fieldset>
<fieldset id="text">
<legend>Message:</legend>
<label for="message"></label>
<textarea id="message" name="message"><?php echo htmlentities($user_message) ?></textarea>
</fieldset><p><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset"/>
</p>
</form>
我尝试了几次不同的迭代,这就是我恰好在那一刻
答案 0 :(得分:5)
这个if (!isset($_POST['submit']))
你告诉它“如果不设置,.....”
你想要if(isset($_POST['submit']))
(现在告诉它,如果设置,执行条件语句中的所有内容)
N.B。: !
是逻辑not
运营商。
根据jeroen
修改强>:
我在设置正确的条件语句(删除逻辑not
运算符)后测试了您的代码,并且邮件已成功发送和接收。因此,您需要检查您的服务器是否设置为处理邮件,或检查您的垃圾邮件以及服务器日志。
答案 1 :(得分:1)
首先我认为你必须改变
if (!isset($_POST['submit']))
至if (isset($_POST['submit']))
因为它看起来像一个bug