我正在使用类似于Dreamweaver Tutorial所示的联系表单。
除非涉及到CSS,否则我会非常好地遵循他的指示。但是,在将表单链接到我的网站后,我不断收到验证错误:
"请输入您的留言以继续"
即使在我输入消息后也会发生这种情况。我已经两次完成他的两部曲系列,但未能找到答案。
我的代码:
<?php
// Set email variables
$email_to = 'Matt@matthewbrianhawn.com';
$email_subject = 'Someone Contacted You on Your Site';
// Set required fields
$required_fields = array('name','email','comment');
// set error messages
$error_messages = array(
'name' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
<!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" xml:lang="en" lang="en">
<head>
<!-- Contact Form Designed by James Brand @ dreamweavertutorial.co.uk -->
<!-- Covered under creative commons license - http://dreamweavertutorial.co.uk/permissions/contact-form-permissions.htm -->
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="contact/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var nameError = '<?php echo $error_messages['name']; ?>';
var emailError = '<?php echo $error_messages['email']; ?>';
var commentError = '<?php echo $error_messages['comment']; ?>';
</script>
</head>
<body>
<div id="form-main">
<div id="form-div">
<?php if($form_complete === FALSE): ?>
<form class="form" id="form1" action="index.php" method="post">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" value="<?php echo isset($_POST['name'])? $_POST['name'] : ''; ?>" />
<?php if(in_array('name', $validation)): ?><span class="error"><?php echo $error_messages['name']; ?></span><?php endif; ?>
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>
</p>
<p class="text">
<textarea name="text" class="feedback-input" id="comment" placeholder="Comment" ><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea>
<?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue" name="submit"/>
<div class="ease"></div>
</div>
</form>
<?php else: ?>
<div class="thanks_message">
<p>Thank you for your Message!</p>
</div>
<?php endif; ?>
</div>
</body>
</html>
答案 0 :(得分:1)
在您的评论的文本字段定义中
textarea name="text" class="feedback-input" id="comment" placeholder="Comment"
id将其更改为
textarea name="comment" class="feedback-input" id="comment" placeholder="Comment"
这就是为什么它认为它是空的,因为目前它被称为“文本”而不是“评论”
答案 1 :(得分:0)
这只是因为您的$_POST
变量中不存在关键的“评论”。
例如,变量$_POST
的键是输入表单的名称。
尝试使用变量$error_messages