我正在尝试创建一个带有PHP验证的联系表单,其中错误消息可以放置在表单字段的上方,下方或中。现在,所有消息都显示在左上角。我尝试制作 3 节课:error_name,错误电子邮件和error_message,我希望这可以将这些消息放置在我想要的位置,但这没有帮助。有人可以告诉我我哪里出错了,或者我如何实现这一目标。
提前致谢
联系表单如下所示:
<?php require_once 'validation.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Forms</title>
<style type="text/css">
.errorlist, .error input{
border: 1px solid #f00;
background: #fdd;
}
form.cmxform fieldset {
margin-bottom: 10px;
}
form.cmxform legend {
padding: 0 2px;
font-weight: bold;
}
form.cmxform label {
display: inline-block;
line-height: 1.8;
vertical-align: top;
}
form.cmxform em {
font-weight: bold;
font-style: normal;
color: #f00;
}
form.cmxform label {
width: 100%; /* Width of labels */
}
input {
width: 100%;
height: 35px;
}
.contact {
width: 100%;
}
.formfield {
width: 80%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="contact">
<div class="formfield">
<form action="index.php" method="post" class="cmxform">
<p>Please complete the form below. Mandatory fields marked<em>*</em></p>
<fieldset>
<div class="fieldset">
<legend>Delivery Details</legend>
<input id="name" class="error_name" name="name" placeholder="What is your name?" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>"
/>
<div class="error_name"></div>
<input id="email" name="email" placeholder="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
<div class="error_email"></div>
<input id="message" class="error_message" name="message" placeholder="message" value="<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?>" />
<input type="submit" value="Verstuur" />
</div>
</fieldset>
</form>
</div>
</div>
</body>
</html>
验证.php如下所示:
<?php
// We gaan de errors in een array bijhouden
$aErrors = array();
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if ( !isset($_POST['name']) or !preg_match( '~^['w ]{3,}$~', $_POST['name'] ) ) {
$aErrors['name'] = 'Please fill in your name';
}
if ( !isset($_POST['email']) or !preg_match( '~^[a-z0-9][a-z0-9_.'-]*@([a-z0-9]+'.)*[a-z0-9][a-z0-9'-]+'.([a-z]{2,6})$~i', $_POST['email'] ) ) {
$aErrors['email'] = 'Please fill in your e-mail address';
}
if ( !isset($_POST['message']) or !preg_match( '~^['w'd ]{28,}$~', $_POST['message'] ) ) {
$aErrors['message'] = 'What would you like to share?';
}
if ( count($aErrors) == 0 ) {
header('Location: http://www.phpfreakz.nl/someotherpage.php');
exit();
}
}
if ( isset($aErrors['name']) and count($aErrors['name']) > 0 ) {
print '<div class="error_name">';
if ( $aErrors['name'] ) {
print '<div>' . $aErrors['name'] . '</div>';
}
print '</div>';
}
if ( isset($aErrors['email']) and count($aErrors['email']) > 0 ) {
print '<div class="error_email">';
if ( $aErrors['email'] ) {
print '<div>' . $aErrors['email'] . '</div>';
}
print '</div>';
}
if ( isset($aErrors['message']) and count($aErrors['message']) > 0 ) {
print '<div class="error_message">';
if ( $aErrors['message'] ) {
print '<div>' . $aErrors['message'] . '</div>';
}
print '</div>';
}
?>
不要立即打印错误,而是将它们保留在数组中,稍后打印。首先在验证中添加空字符串.php以确保不会打印不存在的值。名称错误示例:
<?php
// We gaan de errors in een array bijhouden
$aErrors = array();
$aErrors['name'] = '';
//..Rest of your validation script here
然后在联系表单中,更改以下内容:
<div class="error_name"></div>
对此:
<div class="error_name"><?php print $aErrors['name'];?></div>
Ps:我没有足够的观点可以评论,但你是对的,永远不要依赖表单的客户端评估,因为每个用户都可以禁用 javascript。最好的方法是同时执行这两项操作,但是如果您不想这样做,请始终保留php验证。