我正在构建一个简单的 PHP 联系表单,并且我的错误消息确实以我想要的方式显示在每个表单字段旁边,但它们也出现在表单的顶部,我在其中编写了包含 PHP 代码HTML 文件。我想摆脱包含代码所在的顶部显示的错误。这是包含代码:
<?php include("includes/form.inc.php"); ?>
这是我拥有的 html 表单代码:
<?php echo $finalMsg; ?>
<form method="post" action="contact.php">
<fieldset>
<ul>
<li class="instructions">(*) Required Fields</li>
<li>
<p><?php echo $errors["firstname"]; ?></p>
<label for="firstname" class="lined_up"><span class="asterisk">*</span> First Name:</label>
<input type="text" placeholder="" name="firstname"
id="firstname" value="<?php echo $_POST["firstname"]; ?>" />
<!-- This keeps the content you enter in this field in place if you miss a field & have to resubmit-->
</li>
<li>
<p><?php echo $errors["lastname"]; ?></p>
<label for="lastname" class="lined_up"><span class="asterisk">*</span> Last Name:</label>
<input type="text" placeholder="" name="lastname"
id="lastname" value="<?php echo $_POST["lastname"]; ?>" />
</li>
<li>
<p><?php echo $errors["email"]; ?></p>
<label for="email" class="lined_up"><span class="asterisk">*</span> Email:</label>
<input type="text" placeholder="Please type a valid email." name="email"
id="email" value="<?php echo $_POST["email"]; ?>" />
</li>
<li>
<p><?php echo $errors["message"]; ?></p>
<label for="message" class="message lined_up"><span class="asterisk">*</span> Message:</label>
<textarea id="message" name="message" placeholder="Please enter your message here."><?php echo isset($_POST['message'])? $_POST['message'] : ''; ?></textarea>
</li>
<li>
<input type="submit" value="Send" name="submit" id="submit" />
</li>
</ul>
</fieldset>
</form>
这是我拥有的 PHP 代码:
<?php
//
if(isset($_POST["submit"]))
{
//This is checking if the string length in the firstname field was greater than 1 character
if(strlen($_POST["firstname"]) > 1)
{
$firstname = $_POST["firstname"];
//echo $firstname;
}
else
{
//echo "You did not type in first name";
$errors["firstname"] = "<span class=\"error\">You did not type in a first name.</span>";
}
if(strlen($_POST["lastname"]) > 1)
{
$lastname = $_POST["lastname"];
}
else
{
$errors["lastname"] = "<span class=\"error\">You did not type in a last name.</span>";
}
if(strlen($_POST["email"]) > 1)
{
$email = $_POST["email"];
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email = $email;
}
else {
$errors["email"] = "<span class=\"error\">Email is invalid.</span>";
}
}
else
{
$errors["email"] = "<span class=\"error\">You did not type in an email address.</span>";
}
//This is checking if the string length in the message field was greater than 1 character
if(strlen($_POST["message"]) > 1)
{
$message = $_POST["message"];
//echo $firstname;
}
else
{
$errors["message"] = "<span class=\"error\">You did not type in a message.</span>";
}
// Code to tell form NOT to send form if there are any errors.
if($errors < 1)
{
$to = "liz@lizshannon.com";
$from = $email;
// headers makes sure you have a reply-to address
$headers = "From: {$from}" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$subject =
"From: ($from)" . "<br />" .
"First name: ($firstname)" . "<br />" .
"Last name: ($lastname)" . "<br />" .
"Email: ($email)" . "<br />" .
"Message: ($message)";
if(mail($to, $from, $subject, $headers))
{
$finalMsg = "<p class=\"success\">Thank you! Your email was sent.</p>";
}
else {
$finalMsg = "<p class=\"error\">Your email was NOT sent. Please try again.</p>";
}
}
?>
<?php
//each error is displayed
foreach($errors as $value) {
echo "<span>$value</span><br />";
}
}
?>
任何帮助,将不胜感激。谢谢!