【问题标题】:$error_message isn't showing when checkbox is empty当复选框为空时,$error_message 不显示
【发布时间】:2018-10-08 23:33:08
【问题描述】:
<form name="contactform" method="post" action="send_form_email.php">

<!--Name input-->
<input placeholder="Full Name *" type="text" name="name" maxlength="50">
<br />      

<!--Email input-->
<input placeholder="Email Address *" type="email" name="email" maxlength="80">
<br />

<!--Phone number input (not required)-->
<input placeholder="Telephone Number" type="tel" name="telephone" maxlength="15">
<br />

<!--Company name input (not required)-->
<input placeholder="Company" type="text" name="company" maxlength="50">
<br />

<!--Comments & messege input-->
<textarea  placeholder="Please include as much information as possible *" name="comments" maxlength="500" cols="25" rows="5"></textarea>
<br />

<!--Check for privacy policy-->
<label class="GDRP">
I consent to having this website store my submitted information so they can respond to my inquiry.
  <input type="checkbox" name="GDRP">
</label>

<!--Submit button-->
<input type="submit" value="SUBMIT">

</form>

这是我的 HTML - 我不知道它对帮助我找到解决方案有多重要,但我想,为什么不呢。我试着把它清理干净,这样你就可以尽快帮助我。唯一重要的字段是“姓名”、“电子邮件”、“cmets”和问题“GDRP”

<?php if(isset($_POST['email'])) {

                // EDIT THE 2 LINES BELOW AS REQUIRED
                $email_to = "email@adress.com";
                $email_subject = "www.Adress.com - CONTACT FORM";


                function died($error) {
                    // error code can go here
                    echo "We are very sorry, but there were error(s) found with the form you submitted: ";
                    echo $error."<br />";
                    echo "Please fix these errors.";
                    die();
                }

                // validation expected data exists
                if(
                    !isset($_POST['name']) ||
                    !isset($_POST['email']) ||
                    !isset($_POST['company']) ||
                    !isset($_POST['telephone']) ||
                    !isset($_POST['comments']) ||
                    !isset($_POST['GDRP'])
                ) { 
                died('We are sorry, but there appears to be a problem with the form you submitted.'); 
                }

                $name = $_POST['name']; // required
                $email_from = $_POST['email']; // required
                $telephone = $_POST['telephone']; // not required
                $company = $_POST['company']; // not required
                $comments = $_POST['comments']; // required
                $GDRP = $_POST['GDRP']; // required

                $error_message = "";
                $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
                $string_exp = "/^[A-Za-z .'-]+$/";
              if(!preg_match($string_exp,$name)) {
                $error_message .= '<br /><br /><font color="red">Full Name</font><br /><br />';
              }
              if(!preg_match($email_exp,$email_from)) {
                $error_message .= '<font color="red">Email Address</font><br /><br />';
              }
              if(strlen($comments) < 2) {
                $error_message .= '<font color="red">Comments</font><br /><br />';
              }
              if(!empty($GDRP)) {
                $error_message .= '<font color="red">GDRP Agreement</font><br /><br />';
              }

              if(strlen($error_message) > 0) {
                died($error_message);
              }
                $email_message = "Form details below.\n\n";

                function clean_string($string) {
                  $bad = array("content-type","bcc:","to:","cc:","href");
                  return str_replace($bad,"",$string);
                }

                $email_message .= "First Name: ".clean_string($name)."\n";
                $email_message .= "Email: ".clean_string($email_from)."\n";
                $email_message .= "Company: ".clean_string($company)."\n";
                $email_message .= "Telephone: ".clean_string($telephone)."\n";
                $email_message .= "Comments: ".clean_string($comments)."\n";


            // create email headers
            $headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion();
            @mail($email_to, $email_subject, $email_message, $headers);  
        ?>

        <body>
        Thank you for contacting us. We will be in touch with you as soon as possible.
        </body>

        <?php
        }
        ?>

我的问题是复选框 ('GDRP') 在未填写时不显示 $error_message。 事实上,当 'GDRP' 为空时,没有显示。如果您填写“GDRP”但将其他 [必填字段] 留空,则会显示除“GDRP”之外的所有 $error_message。

【问题讨论】:

    标签: php html forms checkbox


    【解决方案1】:
    if(!empty($GDRP)) { // if not empty
        $error_message .= '<font color="red">GDRP Agreement</font><br /><br />';
    }
    

    看起来您正在检查 $GDPR 是否为空,这应该是相反的。

    if(!empty($GDRP)) { 更改为if(empty($GDRP)) {

    【讨论】:

      【解决方案2】:

      只有选中的复选框才会发送并在 POST 中可用。因此,在以下几行中,您的脚本已完成并且未显示其他错误:

                        !isset($_POST['GDRP'])
                      ) { 
                      died('We are sorry, but there appears to be a problem with the form you submitted.'); 
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        相关资源
        最近更新 更多