$ error_message не отображается, когда флажок пуст - PullRequest
0 голосов
/ 08 октября 2018
<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 - я не знаю, насколько важно помочь мне найти решение, но я решил, почему бы и нет.Я попытался убрать это, чтобы вы могли помочь мне как можно быстрее.Единственными важными полями являются «имя», «электронная почта», «комментарии» и вопрос по существу «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', но оставите другие [обязательные поля] пустыми, все их $ error_message, кроме 'GDRP', будут отображаться.

Ответы [ 2 ]

0 голосов
/ 08 октября 2018

Только отмеченные флажки отправляются и доступны в POST.Итак, в следующих строках ваш скрипт завершен, а другие ошибки не показаны:

                  !isset($_POST['GDRP'])
                ) { 
                died('We are sorry, but there appears to be a problem with the form you submitted.'); 
                }
0 голосов
/ 08 октября 2018
if(!empty($GDRP)) { // if not empty
    $error_message .= '<font color="red">GDRP Agreement</font><br /><br />';
}

похоже, что вы проверяете, что $ GDPR не пусто, это должно быть наоборот.

измените if(!empty($GDRP)) { на if(empty($GDRP)) {.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...