0

我正在尝试使用发送到特定电子邮件地址的表单来设置一个简单的网站。这个想法是在电子邮件中发送一些不同的内容,例如:名字和姓氏、年龄、电子邮件、城市、主题(这是一个带有几个不同选项的下拉菜单),最后是一个评论框。

实际上,在过去的 14 个小时里,我一直在阅读和寻找如何让 PHP 脚本正常工作以便发送表单的方法。但由于某种原因,我就是想不通。

我似乎无法获得包含我需要包含的所有内容的表格。有人可以指出为什么下面的代码不起作用吗?

<?php
/* EMAIL RECIPIENT */
$myemail = "name@example.com";

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

/* Check all form inputs using check_input function */
$formName = check_input ($_POST['formName'], "Please enter your First Name");
$formSurname = check_input ($_POST['formSurname'], "Please enter your First Name");
$formAge = check_input ($_POST['formAge'],);
$formEmail = check_input ($_POST['formEmail'], "Please enter your email so we can get back to you");
$formIntent = check_input ($_POST['formIntent'], "Pleas choose a reason for why contact us");
$formIntent = " (MYFORM) " . $formIntent;
$formComment = check_input ($_POST['formComment']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $formEmail))
{
show_error("E-mail address not valid");
}

/* The layout for the email and how it will be set up for reading the email. */

$message = "Hello!

Your contact form has been submitted by:

Name: $formName
Age: $formAge
City: $formCity
Email: $formEmail
Comment: $formName
The reason for contact with Mistral is: $formIntent
Comment: $formComment

End of message.
";

/* Send the message using the mail() function */
mail($myemail, $subject, $message);

/* Redirect visitors to the thank you page */
header('Location: ../thanks.html');


function show_error($myError)
{?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>

<?php
exit();
}
?>
4

1 回答 1

0

好吧,你验证$_POST['formSurname']了但你从不使用它。同样,您$formCity在您的输出中,$message但您从未定义它。

这很可能是您的问题的原因。

于 2013-02-13T22:28:36.863 回答