有人可以告诉我为什么这不显示表格吗?如果我转到我托管的地址,页面会显示“您的消息已发送。谢谢,”。它似乎只是执行并显示最后一个函数。
<?php
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($retval);
}
return($retval);
}
function validateEmail($data, $fieldName) {
global $errorCount;
if (empty($data)) {
echo "\"$fieldName\" is a required field.<br />\n";
++$errorCount;
$retval = "";
} else { // only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripsplashes($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 $Sender; ?>" /></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" /> <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 = validateInput($_POST['Email'],"Your E-mail");
$Subject = validateInput($_POST['Subject'],"Subject");
$Message = validateInput($_POST['Message'],"Message");
if ($errorCount == 0)
$ShowForm = FALSE;
else
$ShowForm = TRUE;
}
if ($ShowForm == TRUE)
if ($errorCount>0) {// if there were errors
echo "<p>Please re-enter the form information below.</p>\n";
displayForm($Sender, $Email, $Subject, $Message);
} else {
$SenderAddress = "$Sender <$Email>";
$Headers = "From: $SenderAddress\nCC: $SenderAddress\n";
// Substitute your own e-mail address for recipient@example.com
$result = mail("recipient@example.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";
}
?>
答案 0 :(得分:2)
你有:
$showForm = TRUE;
当表单尚未提交时,将导致条件语句始终为$errorCount == 0
。您不会在该条件的其他情况下调用displayForm()
。
希望这有帮助。
答案 1 :(得分:0)
好的,错误是因为大括号应该在if($ ShowForm == TRUE)之后而不是if($ errorCount&gt; 0)之后。