我有以下PHP脚本,在单击发送按钮时运行。
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork@gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit']) {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
&GT;
我正在使用MAMP在本地托管网站。点击发送按钮时会出现问题,而不是发送电子邮件的页面显示代码。
我的代码有什么问题,我该如何解决?
谢谢。
答案 0 :(得分:3)
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork@gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
您的代码中有很多错误
if
和else if
中的条件相同,因此else if
条件永远不会执行。isset($_POST['NAME'])
。