如何正确地将CSS应用于PHP回声?

时间:2019-05-06 03:12:06

标签: php html css ajax

我提交联系表格以采用其适当的CSS后,看不到成功消息。

步骤如下:

  1. 用户将其数据输入联系人表格。

  2. 该表单信息定向到外部PHP脚本,该脚本将信息发送到电子邮件地址。

  3. 提交表单后,PHP会以DIV元素的形式出现回显,其内容类似于“您成功!”。 (到目前为止,这是在嘲笑我)。

  4. 为DIV元素分配了一个“成功”类,该类最终应应用相应类的CSS

除回声外,表单上的所有功能均正常运行。当应用适当的类时,DIV元素没有实际更改。简单来说,我希望文本为绿色,但保持黑色。

HTML表单:


          <div class="col-lg-5 col-md-8">
            <div id="sendmessage" ></div>
            <div id="errormessage"></div>
            <div class="form">
              <form action="php/contactform.php" method="post" role="form" class="contactForm" id="contactForm">
                <div class="form-group">
                  <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" required />
                </div>
                <div class="form-group">
                  <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" required />
                </div>
                <div class="form-group">
                  <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" required />
                </div>
                <div class="form-group">
                  <textarea class="form-control" name="message" id="message" rows="5" data-rule="required" required placeholder="Message"></textarea>
                </div>
                <div class="text-center"><button type="submit" name="submit">Send Message</button></div>
              </form>
            </div>
          </div>

CSS:

#contact .form .success {
  padding: 1em;
  margin-bottom: 0.75rem;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  color: #468847;
  background-color: #dff0d8;
  border: 1px solid #d6e9c6;
  -webkit-border-radius: 4px;
     -moz-border-radius: 4px;
          border-radius: 4px;
}

#contact .form .error {
  padding: 1em;
  margin-bottom: 0.75rem;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  color: #b94a48;
  background-color: #f2dede;
  border: 1px solid rgba(185, 74, 72, 0.3);
  -webkit-border-radius: 4px;
     -moz-border-radius: 4px;
          border-radius: 4px;
}

PHP:

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get the form fields and remove whitespace.
        $name = strip_tags(trim($_POST["name"]));
                $name = str_replace(array("\r","\n"),array(" "," "),$name);
        $subject = strip_tags(trim($_POST["subject"]));
                $subject = str_replace(array("\r","\n"),array(" "," "),$subject);
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $message = trim($_POST["message"]);

        // Check that data was sent to the mailer.
        if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
        }

        // Set the recipient email address.
        // FIXME: Update this to your desired email address.
        $recipient = "me@cbonilla.dev";

        // Set the email subject.
        $subject = "Subject: $subject";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Message:\n$message\n";

        // Build the email headers.
        $email_headers = "From: $name <$email>";

        // Send the email.
        if (mail($recipient, $subject, $email_content, $email_headers)) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo "Thank You! Your message has been flarged.";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message.";
        }

    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }

?>

我不会链接到Ajax,因为它已经应用了适当的类。

我将继续寻找迭代,但是我认为这归结为“您只是不知道CSS”问题。欢迎对代码进行任何批评,无论大小如何或微不足道。

1 个答案:

答案 0 :(得分:3)

您正在尝试将success类附加到sendmessage。为此,您的CSS内容应如下所示,

#contact #sendmessage.success {color:green;}