在阅读了关于这个问题的所有其他文章之后,这对我来说没有帮助。我已经创建了一个联系人表单,html很好,我认为php中缺少一些东西,我需要它后它提交显示“你的电子邮件已成功发送消息。此外,我收到的电子邮件提交后没有显示发布的信息,也不确定如何做到这一点。
/* Logic Script */
<?php
// define variables and set to empty values
$first_nameErr = $emailErr = $last_nameErr = $commentsErr = "";
$first_name = $last_name = $email = $comments = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first_name"])) {
$first_nameErr = "First Name is required";
} else {
$name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["last_name"])) {
$last_nameErr = "Last Name is required";
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$last_nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comments"])) {
$comments = "";
} else {
$comments = test_input($_POST["comments"]);
}
}
//Code for sending the e-mail//
$email = $_POST["email"];
$to = "jason.cameron@myaolcollege.com";
$subject = "Canada Algae E-mail Contact";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email
Name: $first_name $last_name
Comments: $comments";
print("Your form was submitted successfully");
mail($to,$subject,$message,$headers);
?>
html:
/* display code */
<div class="border"></div>
<body>
<div id="wrapper">
<div id="header">
<header class="nav">
<img src="images/CA_Logo.png" alt="Canada Algae Logo">
<a href="#" class="flt_r">
Contact Us
</a>
</header>
</div>
<div id="main">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" id="contact_form">
<label for="first_name">First Name</label>
<input type="text" name="first_name">
<span class="error"> <?php echo $first_nameErr;?> </span>
<br />
<label for="last_name">Last Name</label>
<input type="text" name="last_name">
<span class="error"> <?php echo $last_nameErr;?> </span>
<br />
<label for="email">E-mail</label>
<input type="text" name="email">
<span class="error">
<?php echo $emailErr;?>
</span>
<br />
<label for="comments">Comments</label>
<textarea name="comments">Type your comments here</textarea>
<input type="submit" value="submit">
</form>
</div>
<div id="footer">
<p>Contact Us!</p>
</div>
</div>
</body>
<div class="border"></div>
</html>
这是一个文件。
答案 0 :(得分:0)
在你的PHP中 - 你有
$name = test_input($_POST["name"]);
但在你的html中,输入被称为first_name。
您需要将PHP更改为:
$name = test_input($_POST["first_name"]);
另外 - 您是否实际显示了表单帖子中的名称和评论 - 您似乎只使用了电子邮件地址。