.php只将表单的一部分发送到电子邮件地址

时间:2013-02-13 22:17:58

标签: php forms

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

我实际上花了最近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();
}
?>

1 个答案:

答案 0 :(得分:0)

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

这很可能是导致问题的原因。