PHP联系表单返回代码

时间:2014-12-15 10:56:41

标签: php contact-form

我有以下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在本地托管网站。点击发送按钮时会出现问题,而不是发送电子邮件的页面显示代码。

我的代码有什么问题,我该如何解决?

谢谢。

1 个答案:

答案 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>';
}
?>

您的代码中有很多错误

  1. ifelse if中的条件相同,因此else if条件永远不会执行。
  2. 在检查提交后的值时,使用isset($_POST['NAME'])