2017-02-08 134 views
1

我有一個驗證我的聯繫表格,在需要的輸入下添加一個紅色的文本字體。但我真正想要的是在輸入的底部添加一個紅色的邊界底部以顯示它需要被需要。我有一個已經工作的開關的情況下,但無法找到答案在任何地方添加CSS類到不同的情況。我希望有人比我更瞭解這一點。php開關案例改變css類

這裏是我的PHP

<?php 
session_start(); 
    // define variables and set to empty values 
    $nameErr = $emailErr = $phoneErr = $humanErr = ""; 
    $Name = $Email = $Phone = $Human = ""; 

    $hasError = false; 
    $sent = false; 

    if(isset($_POST['submit'])) { 
     $Name    = trim(htmlspecialchars($_POST['Name'], ENT_QUOTES)); 
     $FName    = trim($_POST['FiancesName']); 
     $Email    = trim($_POST['Email']); 
     $DesiredWedDate  = trim($_POST['DesiredWedDate']); 
     $WeddingSize   = trim($_POST['WeddingSize']); 
     $Phone    = trim($_POST['Phone']); 
     $IndoorCeremony  = trim($_POST['IndoorCeremony']); 
     $OutdoorCeremony  = trim($_POST['OutdoorCeremony']); 
     $AlcoholYes   = trim($_POST['AlcoholYes']); 
     $AlcoholNo   = trim($_POST['AlcoholNo']); 
     $Human    = trim($_POST['Human']); 
     $Number    = 6;   







     $fieldsArray = array(
      'Name' => $Name, 
      'Email' => $Email, 
      'Phone' => $Phone, 
      'Human' => $Human 
     ); 

     $errorArray = array(); 

     foreach($fieldsArray as $key => $val) { 
      switch ($key) { 
       case 'Name': 
        if(empty($val)) { 
         $hasError = true; 
         $nameErr = "Please enter your name."; 
        } 
       case 'Name': 
        if (!preg_match("/^[a-zA-Z ]*$/", ($val))) { 
         $hasError = true; 
         $nameErr = "The value must be alphanumeric."; 
        } 
        break; 
       case 'Phone': 
        if (!preg_match("/^[0-9]+$/", ($val))) { 
         $hasError = true; 
         $phoneErr = "Only numbers and white space allowed."; 
        } 
       case 'Phone': 
        if(empty($val)) { 
         $hasError = true; 
         $phoneErr = "Phone is required."; 
        } 
        break; 
       case 'Email': 
        if(!filter_var($Email, FILTER_VALIDATE_EMAIL)) { 
         $hasError = true; 
         $emailErr = "Email is required."; 
        } else { 
         $Email = filter_var($Email, FILTER_SANITIZE_EMAIL); 
        } 
        break; 
       case 'Human': 
        if (!preg_match("/[^\d]?6[^\d]?/", ($val))) { 
         $hasError = true; 
         $humanErr = "Not the right answer"; 
        } 
       case 'Human': 
        if (!preg_match("/^[0-9]+$/", ($val))) { 
         $hasError = true; 
         $humanErr = "Must be a number"; 
        } 
       case 'Human': 
        if(empty($val)) { 
         $hasError = true; 
         $humanErr = "Are you human?"; 
        } 
        break; 
      } 
     } 


     //CHECK BOX WRITE UP 
     if (isset($_POST['IndoorCeremony'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     if (isset($_POST['OutdoorCeremony'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     if (isset($_POST['AlcoholYes'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 

     if (isset($_POST['AlcoholNo'])) { 
     $checkBoxValue = "yes"; 
      //is checked 
     } else { 
      $checkBoxValue = "no"; 
      //is unchecked 
     } 


     //Validation Success! 
     //Do form processing like email, database etc here 
     if($hasError !== true) { 
      $priority = $_POST['priority']; 
      $type = $_POST['type']; 
      $message = $_POST['message']; 

      //FOR STYLING EMAIL 
      // $headers .= "MIME-Version: 1.0" . "\r\n"; 
      //$headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n"; 

      //STYLING EMAIL 
      /* $message = "<html> 
         <h1> 
         $Name 
         </h1> 
         <BR> 
         <h3> 
         $Email 
         <BR>Tel: $Phone 
         <BR>Company: $Compnay 
         <BR>Website: $Website 
         <BR>Subject: $Subjectmatter 
         <BR>Describe: $Describe 
         </h3> 
         <BR> 
         <BR> 
         <BR><h4>Web Design: $webdesign 
         <BR>Web Hosting: $webhosting 
         <BR>Wordpress Design: $wordpressdesign 
         <BR>Logo Design: $logodesign 
         <BR>Brochures: $brochures</h4> 
         <BR> 
         <BR> 
         <h4> 
         Other: $otherswitch 
         <BR>Describe: $OtherDescribe 
         </h4> 
         </html>"; 
         */ 

      $formcontent=" From: $Name \n \n Fiance's Name: $FName \n \n Email: $Email \n \n Phone: $Phone \n \n Desired Wedding Date: $DesiredWedDate \n \n Wedding Size: $WeddingSize \n \n Describe: $Describe \n \n Indoor Ceremony: $IndoorCeremony \n \n Outdoor Ceremony: $OutdoorCeremony \n \n Alcohol Yes: $AlcoholYes \n \n Alcohol No $AlcoholNo \n \n Referral: $Referral \n "; 
      $recipient = "[email protected]"; 
      $subject = "Pre Book Wedding Contact Form"; 
      $mailheader = "From: $Email \r\n"; 
      mail($recipient, $subject, $formcontent, $mailheader /*$message, $headers*/); 
      header("Refresh:0; url=thanks.php"); 
      exit();  
     } 
    } 

?><!--END PHP--> 

這裏是我的輸入形式

<span class="input input--kaede"> 
        <input name="FiancesName" class="input__field input__field--kaede" type="text" id="input-2" /> 
        <label class="input__label input__label--kaede" for="input-2"> 
         <span class="input__label-content input__label-content--kaede">Fiance's Name</span> 
        </label> 
       </span> 

我想補充的CSS 我把它叫做

.under_text_error { 
    border-bottom: 2px solid red; 
} 

應該希望給別人足夠離開。我感謝任何可能的幫助!

回答

1

希望這有助於

<?php 
     if(!empty($nameErr)) 
     { 
    ?> 
      <div class="under_text_error"> 
       <?php echo $nameErr ?> 
      </div> 
    <?php 
     } 
    ?> 
+0

沒問題,但問題是當我提交。紅色下劃線styas和裏面的輸入消失。 –

+0

對不起。你可以說清楚..什麼意思? – affaz

+0

是的沒問題。我在span類和輸入名稱之間添加了該代碼。現在,當我點擊提交時,紅色下劃線顯示出來。當我填寫信息並重新提交時,該行仍然保留並且輸入的信息消失。 –

0

只是呼應了錯誤類,如果現場有錯誤

喜歡的東西:

<input class="input__field input__field--kaede <?=isset($nameErr)? 'under_text_error':''?>" /> 
+0

我試過了。這不起作用的原因 –

0

好這裏是我的嘗試。

<span class="input input--kaede <?php echo $nameErr ?>"> 
        <?php 
         if(!empty($nameErr)) 
         { 
        ?> 
         <div class="under_text_error"> 

         </div> 
        <?php 
         } 
        ?> 
        <input name="Name" class="input__field input__field--kaede " type="text" id="input-2" /> 
        <label class="input__label input__label--kaede " for="input-2" value="<?php echo (isset($Name) ? $Name : ""); ?>"> 
         <span class="input__label-content input__label-content--kaede ">Fiance's Name</span> 
        </label> 
       </span> 

什麼這最終做的是把輸入的紅線,當我填寫好並提交消失喜歡它的猜想。現在的問題是,輸入信息(你輸入的內容消失了)不保留它。

+0

你可以把輸出的截圖 – affaz

1

下面是解

<span class="input input--kaede <?php echo $FnameErr ?>"> 
<?php 
if(!empty($FnameErr)) 
{ 
?> 
<div class="under_text_error"> 

</div> 
<?php 
} 
?> 
<input name="FName" class="input__field input__field--kaede " type="text" id="input-2" value="<?php echo (isset($FName) ? $FName : ""); ?>"/> 
<label class="input__label input__label--kaede " for="input-2"> 
<span class="input__label-content input__label-content--kaede ">Fiance's Name</span> 
</label> 
</span> 
0

這裏是您填寫或提交 enter image description here

忽略頂衝前。我只是測試其他選項。但正如你所看到的,這條線在提交之後在這裏工作並且沒有答案。 enter image description here

這裏是一個填充答案的照片。一旦提交它看起來像圖像1,但空白。 enter image description here

下面是它在提交後最後看起來像什麼。所以它忘記了 enter image description here

+0

你檢查了我最近發佈的方式> – affaz

+0

是的,我做到了。我得到同樣的東西。這只是複雜的嗎?我應該嘗試其他的東西嗎? –

+0

nops..dont放棄..是你的當前php代碼相同發佈在你的問題? – affaz