php switch case改变css类

时间:2017-02-08 17:13:05

标签: php jquery session switch-statement case

我对我的联系表单进行了验证,在所需输入下添加了红色文本字体。但我真正想要的是在输入的底部添加一个红色边框底部以显示它需要。我有一个已经工作的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 = "Youremail@email.com";
            $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;
}

这应该有希望给一个足够的人。我感谢任何可能的帮助!

5 个答案:

答案 0 :(得分:1)

希望这有帮助

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(self.handler(notification:)), name: NSNotification.Name.init(rawValue: "Post"), object: nil)

}

func handler(notification: Notification) {

    let alertController = UIAlertController(title: "Hello", message: nil, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
    self.present(alertController, animated: true, completion: nil)

}

答案 1 :(得分: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> 

答案 2 :(得分:0)

如果字段有错误,只需回显错误类

类似的东西:

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

答案 3 :(得分: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>

这最终做的是在输入上放置红线,当我填写并提交时,它就像它的假设一样消失。现在的问题是输入信息(你键入的内容消失了)它没有保留它。

答案 4 :(得分:0)

在您填写或提交之前 enter image description here

忽略顶部的红色。我只是测试其他选项。但正如你可以看到这条线在提交后没有答案。 enter image description here

这是一个填写答案的镜头。一旦提交,它看起来像图像1,但是空白。 enter image description here

这是提交后最后的样子。所以忘了 enter image description here