PHP联系表单问题(红色边框验证)

时间:2013-12-05 06:21:11

标签: php html validation web contact-form

我已经在网上搜索了两天的某种PHP模板(因为我不知道如何用PHP编写代码),如果用户没有输入所有正确的信息,可能会突出显示边框红色。我设法让联系表格工作但不是我想要的方式。基本上,我需要用户输入适当的信息;如果没有,则出现红色边框。

现状: [基本联系表格:无验证]

期望情况: [验证联系表格] *

  1. 名称(必填)(如果错误:红色边框)
  2. 电子邮件(必填:至少包括@符号)(如果错误:红色边框)
  3. 主题(必填)(如果错误:红色边框)
  4. 消息(必填)(如果错误:红色边框)
  5. *

    我的网站是:www.haildark.com,如果您想详细了解它。如果可以的话请帮忙。我想我的大脑即将崩溃。谢谢。它不一定是在PHP中。只要代码完成工作,我就完全没问题。

    HTML:

    <!-- Contact -->
    <div class="wrapper wrapper-style5">
      <article id="contact" class="container small">
        <header>
          <h2>
            Contact Us
          </h2>
          <span>
            If you have any questions, comments, or concerns, please contact below and we will respond as soon as we can.
            Please allow us at least 72 hours to respond. Thanks for supporting us.
          </span>
        </header>
        <div>
          <div class="row">
            <div class="12u">
              <form method="post" action="contact.php">
                <div>
                  <div class="row half">
                    <div class="6u">
                      <input type="text" name="name" id="name" placeholder="Name" />
                    </div>
                    <div class="6u">
                      <input type="text" name="email" id="email" placeholder="Email" />
                    </div>
                  </div>
                  <div class="row half">
                    <div class="12u">
                      <input type="text" name="subject" id="subject" placeholder="Subject" />
                    </div>
                  </div>
                  <div class="row half">
                    <div class="12u">
                      <textarea name="message" id="message" placeholder="Message">
                      </textarea>
                    </div>
                  </div>
                  <div class="row">
                    <div class="12u">
                      <a href="#" class="button form-button-submit">
                        Send Message
                      </a>
                      <a href="#" class="button button-alt form-button-reset">
                        Clear Form
                      </a>
                    </div>
                  </div>
                </div>
              </form>
            </div>
          </div>
    

    基本PHP:

    <?php
    $field_name = $_POST['name'];
    $field_email = $_POST['email'];
    $field_subject = $_POST['subject'];
    $field_message = $_POST['message'];
    
    $mail_to = 'info@domain.com';
    $subject = 'HailDark® User: '.$field_subject." - ".$field_name;
    
    $body_message = 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Subject: '.$field_subject."\n";
    $body_message .= 'Message: '.$field_message;
    
    $headers = 'From: '.$field_email."\r\n";
    $headers = 'Reply To: '.$field_email."\r\n";
    
    $mail_status = mail($mail_to, $subject, $body_message, $headers);
    
    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'index.html';
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to info@domain.com');
            window.location = 'index.html';
        </script>
    <?php
    }
    ?>
    

2 个答案:

答案 0 :(得分:1)

I have seen that you are using HTML5 but not completely.You used <input type="text" name="email" id="email" placeholder="Email" />

You can use <input type="email" name="email" id="email" placeholder="Email" required="required"/>

Html5 provides powerful way to manage your site like as the above line I used type="email" it automatically validation for email and also required="required" attribute provides you this field must not be empty it will highlighted you.
So use at first <!DOCTYPE html>before <html> tag and use html5 tags and use required="required" attribute for every tag where you want to validate the field.

答案 1 :(得分:0)

<?php
if(empty($_POST['test'])) // other validation
{
    $error = true;
}
?>


<input type="text" name='test' class='<?php echo ($error)?"red-border":"green-border"; ?>' />
<input type='submit' value='send' />

<style type="text/css">
    .red-border{
        border: 1px solid red;
    }
    .green-border{
        border: 1px solid green;
    }
</style>