表格没有显示?

时间:2015-10-11 06:56:28

标签: php forms

我需要一些帮助,希望你们中的一个能为我解答。我有一个PHP课程作业。对于这项任务,我们的任务是制作一个包含4个输入字段的表单(包括重置和提交按钮)。这些字段应标记为名称,地址,电子邮件和电话号码。现在,当我将代码写入Dreamweaver时,我没有语法错误,但每当我使用Wamp执行脚本时,表单都不显示。任何帮助都会受到赞赏,因为这是我们中期必须要做的事情。

<?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 = stripslashes($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 = 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, $Address, $Phone) {
?>
<h2 style= "text-align:center">Contact Us</h2>
<form name="contact" action="contact_us.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>Address: <input type="text" name="Address" value="<?php echo $Address; ?>" /></p>
<p>Phone #: <input type="number" name="Phone" value="<?php echo $Phone; ?>"<br /> 
</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 = "";
$Address = "";
$Phone = "";
if (isset($_POST['Submit'])) {
    $Sender = 
    validateInput($_POST['Sender'], "Your Name");
    $Email = 
    validateEmail($_POST['Email'], "Your E-mail");
    $Subject = 
    validateInput($_POST['Address'], "Your Address");
    $Message = 
    validateInput($_POST['Phone'],"Your number");
    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, $Address, $Phone);
}
else {
    $SenderAddress= "$Sender <$Email>";
    $Headers= "From: $SenderAddress\nCC:
    $SenderAddress\n";
    // Substitute your own email 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";
} ?>

2 个答案:

答案 0 :(得分:0)

if ($ShowForm == TRUE) {
    if ($errorCount>0) // if there were errors echo "<p>Please re-enter the form information below.</p>\n"; 
    displayForm($Sender, $Email, $Address, $Phone);
}

看起来您的表单设置为仅在$ShowForm == TRUE && $errorCount > 0时显示。由于您的默认设置为$ShowForm = TRUE and $errorCount = 0,因此您的表单永远不会显示。

这就是为什么在ifs上使用花括号,同时使文件更大,在排除故障时会有很大帮助。

答案 1 :(得分:0)

你什么也没看到,因为你只需要调用函数displayForm()来创建它。

所以把这个displayForm();在你的php标签结束之前,看看会发生什么。