我可以使用PHP表单配置错误div。 我想有一个简单的div显示表格下面的错误。
这是我的代码:
PHP
<?php
if (isset($_REQUEST['submitted'])) {
$errors = array();
if (!empty($_REQUEST['firstname'])) {
$firstname = $_REQUEST['firstname'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";
if (preg_match($pattern,$firstname)){
$firstname = $_REQUEST['firstname'];
} else {
$errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';
}
} else {
$errors[] = 'You forgot to enter your First Name.';
}
}
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From";
$to = "mail@mail.com";
$subject = "test" . $name . "";
$message = "Message from " . $firstname . "
mail($to,$subject,$message,$from);
header('Location: http://www.google.com');
}
}
?>
HTML
<form method="post" action="contactform.php">
<div class="field">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" />
</div>
<ul class="actions">
<li>
<input type="submit" input name="submitted" value="Send Message" />
</li>
</ul>
</form>
非常感谢!