2011-06-22 120 views
0

好吧我試圖讓這個表格到如果有人跳過表單上的東西,它會出現並告訴他們。我是一名新手,如果有人不輸入信息,我不知道如何解決這個錯誤。這是我關於聯繫表格

<?php 
function showForm($strMessage){ 
echo "<h1>".$strMessage."</h1>"; 
echo " <p>Note: fields marked with '*' are required</p>\n"; 
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n"; 
echo "<table width=\"45%\" class=\"formtable\" cellpadding=\"3\" cellspacing=\"0\">\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvname\">* Name:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"name\" value=\"".$_POST['name']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvemail\">* E-mail:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"email\" value=\"".$_POST['emial']."\" /></td>\n"; 
echo " </tr>\n"; 
echo "  <tr>\n"; 
echo "  <td><span id=\"rfvusername\">* Username:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"username\" value=\"".$_POST['username']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvpword\">* Password:</span></td>\n"; 
echo "  <td><input type=\"password\" name=\"pword\" value=\"".$_POST['pword']."\" /><br /><span style=\"font-size:9px;\"><em>(at least 4 chars) </em></span></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "  <td><span id=\"rfvpword\">* Re-enter Password:</span></td>\n"; 
echo "  <td><input type=\"text\" name=\"repword\" value=\"".$_POST['repword']."\" /></td>\n"; 
echo " </tr>\n"; 
echo " <tr>\n"; 
echo "   <td>&nbsp;</td>\n"; 
echo "   <td><input type=\"submit\" value=\"Submit\" class=\"btnSubmit\" id=\"btnSubmit\" name=\"submit\" /></td>\n"; 
echo " </tr>\n"; 
echo "</table>\n"; 
echo "</form>\n"; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>Contact Form</title> 
    <style type="text/css"> 
    body{ 
     background-color:#FFBD40; 
     color:#000000; 
     font-size:100%; 
     font-family:Georgia,Verdana,"Times New Roman",sans-serif; 
    } 


    #container{ 
     background:#FFF573; 
     width:800px; 
     margin:auto; 
     padding:5px 10px 5px 10px; 
     border:6px double #000000; 
     } 
     </style> 
    </head> 
    <body> 
    <div id="container"> 
    <?php 
    if (isset($_POST['submit'])){ 
    if (trim($_POST['name'])==""){ 
    $strMessage="Please enter your name!"; 
    showForm($strMessage); 
    } 
    elseif (strlen(trim($_POST['pword']))<=3){ 
    $strMessage="Your password must be at least 4 characters long!"; 
    showForm($strMessage); 
    } 
    else{ 
    $strMessage="Thank you, your information has been submitted. Below is the information you sent:"; 
    $strMessageBody.="Name: ".trim(stripslashes($_POST['name']))."<br />"; 
    $strMessageBody.="E-mail: ".trim(stripslashes($_POST['email']))."<br />"; 
    $strMessageBody.="UserName: ".trim(stripslashes($_POST['username']))."<br />"; 
    $strMessageBody.="Password: ".trim(stripslashes($_POST['pword']))."<br />"; 
    $strMessageBody.="Re-enter Password: ".trim(stripslashes($_POST['repword']))."<br />"; 
    echo "<h1>".$strMessage."</h1>"; 
    echo $strMessageBody; 
} 
    } 
else{ 
$strMessage= "Please fill out the form below to send your information:"; 
showForm($strMessage); 
} 
    ?>php 
    $$errors = array(); 
    if (isset($_REQUEST["seen_already"])){ 
    validate_data(); 
    if(count($errors) ! = 0){ 
    display_errors(); 
    display_welcome(); 
    } else { 
    display_welsome(); 
    } 
    function validate_data() 
    { 
    global $errors; 
    if($_REQUEST["Name"]==""){ 
    $errors[] = "<FONT COLOR='RED'>Please enter your first name</FONT>"; 
    } 
    if($_REQUEST["E-mail"]==""){ 
    $error[] = "<FONT COLOR='RED'>Please enter your E-Mail>/FONT>"; 
    } 
    if($_REQUEST["UserName"]==""){ 
      $errors[] = "<FONT COLOR='RED'>Please enter your Username</FONT>"; 
    } 
    if($_REQUEST["Password"]==""){ 
      $errors[] = "<FONT COLOR='RED'>Please enter your Password</FONT>"; 
    } 
    if($_REQUEST["RE-enter Password"]==""){ 
      $errors[] = "<FONT COLOR='RED'>Please re-enter your Password</FONT>"; 
    } 
    } 
    function display_errors() 
    { 
     global $errors; 

     foreach($errors as $err){ 
     echo $err, "<BR>"; 
     } 
     } 
     function process_data() 


</div> 
    </body> 
    </html> 

好回聲的和「和\是我怎麼也得處理這件事的。所以它是假設像她那樣。和所有我是問,如果有人可以幫助我瞭解如何做錯誤代碼,不是爲我做,而是幫助我理解它,是我已經開始的代碼會工作,還是我在做這一切搞砸了,並感謝誰投票給我失望。只是問一個簡單的問題

+0

你能解釋一下有什麼問題嗎? – Blender

+0

我不知道如何真正做錯誤代碼,我相信我很可能不會做正確的。由於某種原因,我認爲我所做的這個錯誤代碼非常長 – andy

+0

Andy,好的IDE(NetBeans)會爲你的代碼做很大的幫助。 –

回答

4

你的問題是這樣的:

$_REQUEST["RE-enter Password"] 

您使用的TEX tual標籤爲表單域。但您的實際輸入字段被稱爲:

$_REQUEST["repword"] 

對於所有其他字段也是如此。 $_REQUEST數組保存根據HTML表單中的name=屬性的鍵。


其他:還可以簡化表單輸出。相反,許多echo S的你應該使用heredoc string避免許多" dquotes和\反斜槓:

echo <<<HTML 
<h1>$strMessage</h1> 
<p>Note: fields marked with '*' are required</p> 
<form action="$_SERVER[PHP_SELF]" method="post"> 
<table ... 
     <td><span id="rfvname">* Name:</span></td> 
    <td><input type="text" name="name" value="$_POST[name]"></td> 
... 
HTML; 

在此之前,還添加$_POST = array_map("htmlentities", $_POST);(爲簡單起見/安全性)。

+0

最後還有多個語法錯誤 –

+0

是的,有山姆因爲我很困惑如何做錯誤代碼,所以我沒有阻止。和「雙引號和\因爲這就是我所想... – andy

+0

最後缺少'}'可能是由於代碼被縮短?我也忽略了HTML錯誤(人造xhtml和字體'validate_data()',雖然不是非常可讀但沒有正確的indendation,但似乎沒有錯。 – mario