【问题标题】:php switch case to change css classphp switch case更改css类
【发布时间】:2017-02-08 17:13:05
【问题描述】:

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

这应该有希望让某人有足够的时间离开。感谢您提供任何可能的帮助!

【问题讨论】:

    标签: php jquery session switch-statement case


    【解决方案1】:

    希望对你有帮助

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

    【讨论】:

    • 好的,但问题是当我提交时。红色下划线 styas 和里面的输入消失了。
    • 是的,没问题。我在跨度类和输入名称之间添加了该代码。现在当我点击提交时,红色下划线出现。当我填写信息并重新提交时,该行仍然存在,输入的信息消失。
    • 不要在这个跨度类中给出输入。根据我们的条件,如果没有错误,它不会显示这个跨度。只在这个类中给出错误信息
    • 在上面我重新发布了我用你的代码尝试过的内容。不完全确定我是否做得对(显然不是)。
    【解决方案2】:

    解决办法

       <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> 
    

    【讨论】:

      【解决方案3】:

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

      类似:

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

      【讨论】:

        【解决方案4】:

        好的,这就是我尝试过的。

        <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>
        

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

        【讨论】:

        • 你能把输出的截图放上去吗
        【解决方案5】:

        这是在您填写或提交之前

        忽略顶部的红色。我只是在测试其他选项。但正如您所看到的,提交后这条线在这里工作并且没有答案。

        这是填写答案的照片。提交后,它看起来像图片 1,但为空白。

        这是提交后最后的样子。所以它忘记了

        【讨论】:

        • 你按照我最近发布的方式检查了吗>
        • 是的,我做到了。我得到同样的东西。这只是为了复杂吗?我应该尝试别的吗?
        • nops..不要放弃..您当前的 php 代码与您的问题中发布的相同吗?
        • 我猜这行 $Name = $Email = $Phone = $Human = ""; 正在将你的值重置为 null..为什么你需要那行?
        • 有人告诉我我需要它。但我只是试图删除它,但还是一样。我放的下面有更多的php,但那是电子邮件。我可以添加它,以便您查看
        猜你喜欢
        • 2011-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多