2013-04-28 40 views
1

我正在構建一個簡單的PHP聯繫表單,並且我的錯誤消息按照我想要的方式顯示在每個表單字段旁邊,但它們也出現在表單頂部,其中包含了PHP代碼在我的HTML文件上。我想擺脫顯示在包含代碼的頂部的錯誤。這是包括代碼:如何在字段旁邊顯示php表單錯誤?

<?php include("includes/form.inc.php"); ?> 

這是HTML表單代碼,我有:

<?php echo $finalMsg; ?> 

     <form method="post" action="contact.php"> 

      <fieldset> 

       <ul> 

        <li class="instructions">(*) Required Fields</li> 

        <li> 
         <p><?php echo $errors["firstname"]; ?></p> 
         <label for="firstname" class="lined_up"><span class="asterisk">*</span> First Name:</label> 
         <input type="text" placeholder="" name="firstname" 
           id="firstname" value="<?php echo $_POST["firstname"]; ?>" /> 
                 <!-- This keeps the content you enter in this field in place if you miss a field & have to resubmit--> 
        </li> 

        <li> 
         <p><?php echo $errors["lastname"]; ?></p> 
         <label for="lastname" class="lined_up"><span class="asterisk">*</span> Last Name:</label> 
         <input type="text" placeholder="" name="lastname" 
          id="lastname" value="<?php echo $_POST["lastname"]; ?>" /> 
        </li> 

        <li> 
         <p><?php echo $errors["email"]; ?></p> 
         <label for="email" class="lined_up"><span class="asterisk">*</span> Email:</label> 
         <input type="text" placeholder="Please type a valid email." name="email" 
          id="email" value="<?php echo $_POST["email"]; ?>" /> 
        </li> 
        <li> 
         <p><?php echo $errors["message"]; ?></p> 
         <label for="message" class="message lined_up"><span class="asterisk">*</span> Message:</label> 
         <textarea id="message" name="message" placeholder="Please enter your message here."><?php echo isset($_POST['message'])? $_POST['message'] : ''; ?></textarea> 

        </li> 
        <li> 
         <input type="submit" value="Send" name="submit" id="submit" /> 
        </li> 
       </ul> 
      </fieldset> 

     </form> 

這是PHP代碼,我有:

<?php 

// 
if(isset($_POST["submit"])) 
{ 

    //This is checking if the string length in the firstname field was greater than 1 character 
    if(strlen($_POST["firstname"]) > 1) 
    { 

     $firstname = $_POST["firstname"]; 
     //echo $firstname; 
    } 

    else 
    { 
     //echo "You did not type in first name"; 
     $errors["firstname"] = "<span class=\"error\">You did not type in a first name.</span>"; 
    } 


    if(strlen($_POST["lastname"]) > 1) 
    { 

     $lastname = $_POST["lastname"]; 
    } 

    else 
    { 
     $errors["lastname"] = "<span class=\"error\">You did not type in a last name.</span>"; 
    } 


    if(strlen($_POST["email"]) > 1) 
    { 

     $email = $_POST["email"]; 

     if(filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $email = $email; 
     } 

     else { 
      $errors["email"] = "<span class=\"error\">Email is invalid.</span>"; 
     } 
    } 

    else 
    { 
     $errors["email"] = "<span class=\"error\">You did not type in an email address.</span>"; 
    } 

    //This is checking if the string length in the message field was greater than 1 character 
    if(strlen($_POST["message"]) > 1) 
    { 

     $message = $_POST["message"]; 
     //echo $firstname; 
    } 

    else 
    { 

     $errors["message"] = "<span class=\"error\">You did not type in a message.</span>"; 
    } 


    // Code to tell form NOT to send form if there are any errors. 
    if($errors < 1) 
    { 
      $to = "[email protected]"; 
      $from = $email; 
      // headers makes sure you have a reply-to address 
      $headers = "From: {$from}" . "\r\n"; 
      $headers .= 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1'; 

      $subject = 
       "From: ($from)" . "<br />" . 
       "First name: ($firstname)" . "<br />" . 
       "Last name: ($lastname)" . "<br />" . 
       "Email: ($email)" . "<br />" . 
       "Message: ($message)"; 

      if(mail($to, $from, $subject, $headers)) 
      { 
       $finalMsg = "<p class=\"success\">Thank you! Your email was sent.</p>"; 
      } 

      else { 
       $finalMsg = "<p class=\"error\">Your email was NOT sent. Please try again.</p>"; 
      } 

} 

?> 

<?php 

//each error is displayed 

foreach($errors as $value) { 

echo "<span>$value</span><br />"; 

} 
    } 
?> 

任何幫助,將不勝感激。謝謝!

+0

做的jQuery插件驗證一個 – 2013-04-28 05:58:00

+0

客戶端檢查很難ŧ o從你的問題中告訴我,但我假設他們顯示爲PHP錯誤。在代碼頂部添加'display_errors(false);'。如果你有權訪問服務器上的php配置,最好在php.ini中關閉它們。它應該始終關閉生產站點 – Cfreak 2013-04-28 06:01:44

回答

1

刪除回波或因爲你不希望立即打印

foreach($errors as $value) { 

echo "<span>$value</span><br />"; 

} 

可以去除整塊的循環,你應該使用jquery或在客戶端的JavaScript驗證這讓時間將減少服務器 - 客戶端通信

+0

您絕不應該完全依賴客戶端驗證。這會打開一個巨大的安全漏洞。 – 2013-04-28 06:08:20

+1

@LoganBailey是的,但你應該檢查客戶端以及服務器端以獲得更高的安全性。我告訴添加客戶端不要刪除服務器端。 – 2013-04-28 06:10:41

+0

@yadav chetan:雙方驗證有什麼好處? ,是否有可能在客戶端和服務器驗證中有不同的結果? – BaSha 2013-04-29 05:06:24

1

只是刪除的代碼塊:

<?php 
    //each error is displayed 
    foreach($errors as $value) { 
    echo "<span>$value</span><br />"; 
    } 
} 
相關問題