2017-02-08 54 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