PHP一体式表单中的电子邮件验证错误



我正在做一本书中的练习,并准确地复制了里面的内容(据我所知(,但我仍然无法处理有效的电子邮件。我试过不同的正则表达式,但没有成功。我很确定我错过了一些愚蠢的东西(要求这个听起来很愚蠢,但我是PHP的新手(。有人能看一眼,看看你是否能看到我错过了什么(或做错了什么(吗?

<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact Me</title>
</head>
<header>
</header>
<body>
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo ""$fieldName" is a required field.<br />n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}


function validateEmail($data, $fieldName) {
global $errorCount;
if(empty($data)) {
echo ""$fieldName" is a required field.<br />n";
++$errorCount;
$retval = "";
} else {
$retval = trim($data);
$retval = stripslashes($retval);
$pattern = "/^[w-]+(.[w-]+)*@" . "[w-]+(.[w-]+)*" . "(.[[a-z]]{2,})$/i";
if (preg_match($pattern, $retval)==0) {
echo ""$fieldName" is not a valid e-mail address.<br />n";
++$errorCount;
}
}
return($retval);
}
function displayForm($Sender, $Email, $Subject, $Message) {
?>
<h2 style = "text-align:center">Contact Me</h2>
<form name=contact: action="ContactForm.php" method="post">
<p>Your Name: <input type="text" name="Sender" value="<?php echo $Sender; ?>" /></p>
<p>Your E-mail: <input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Subject: <input type="text" name="Subject" value="<?php echo $Subject; ?>" /></p>
<p>Message: <br />
<textarea name="Message"><?php echo $Message; ?></textarea></p>
<p><input type="reset" value="Clear Form" />&nbsp;
&nbsp;<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
$showForm = TRUE;
$errorCount = 0;
$Sender = "";
$Email = "";
$Subject = "";
$Message = "";
if (isset($_POST['Submit'])) {
$Sender = validateInput($_POST['Sender'], "Your Name");
$Email = validateEmail($_POST['Email'], "Your Email");
$Subject = validateInput($_POST['Subject'], "Subject");
$Message = validateInput($_POST['Message'], "Message");
if($errorCount==0)
$showForm = FALSE;
else
$showForm = TRUE;
}

if ($showForm == TRUE) {
if ($errorCount>0)
echo "<p>Please re-enter the form information below.</p>n";
displayForm($Sender, $Email, $Subject, $Message);
}
else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddressn CC: $SenderAddressn";
$result = mail("greg.englar@gmail.com", $Subject, $Message, $Headers);
if ($result)
echo "<p>Your message has been sent. Thank you, " . $Sender . ".</p>n";
else
echo "<p>There was an error sending your message, " . $Sender . ".</p>n";
}
?>
</body> 
</html>

我认为我很清楚代码在做什么以及它是如何工作的,但我显然遗漏了一些东西。它不接受任何有效的电子邮件,并且总是显示一条错误消息。作为参考,我正在使用的书是";用MySQL第二版进行PHP编程";唐·戈塞林。

正则表达式中存在错误。在结束时应为[a-z]而不是[[a-z]]

相关内容

  • 没有找到相关文章

最新更新