0

有人能告诉我为什么这不显示表格吗?如果我转到托管它的地址,页面只会显示“您的消息已发送。谢谢,.” 它似乎只是执行并显示最后一个功能。

    <?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" />&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 = 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";
        }
    ?>
4

2 回答 2

2

你有:

$showForm = TRUE;

其中,当表单尚未提交时,将导致条件语句始终为真$errorCount == 0。你不会displayForm()在那个条件的 else 情况下调用。

希望这可以帮助。

于 2012-05-01T20:49:55.180 回答
0

好的,错误是因为大括号应该在 if ($ShowForm == TRUE) 而不是 if ($errorCount>0) 之后。

于 2012-05-02T20:46:05.313 回答