2015-10-11 208 views
1

我需要一些幫助,我希望你們其中一個能爲我解答。我有一個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 // [email protected] 
    $result = mail ("[email protected]", 
    $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

你看不到任何東西,因爲你沒有調用你剛創建的函數displayForm()。

所以把這個displayForm();在你的php標籤結束之前,看看會發生什麼。

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時使用大括號,同時使文件變大的原因,這對故障排除很有幫助。