HTML/PHP 表单无法在 Safari 中提交

HTML/PHP form wont submit in Safari

下面的 HTML 和 PHP 代码按预期在 Google 中执行 Chrome - 在表单中输入请求的数据并按发送,然后发送一条消息到一个电子邮件地址,页面会刷新。

但是,在 Safari 中不会发生相同的预期行为 - 单击 'send' 按钮后,用户将转到空白页面(email.php 文件)。

谁能告诉我为什么它在 Safari 中没有按预期运行?

HTML代码:

<form method="POST" id="contactForm" name="contactForm" class="contactForm" novalidate="novalidate" action="email.php" onsubmit="this.submit(); this.reset(); return false;">

                      <div class="row">

                        <!-- Name Input -->
                        <div class="col-md-1 col-sm-1">
                          <div class="form-group">
                            <label class="contact-text">Name:</label>
                          </div>
                        </div>
                        <div class="col-md-11 col-sm-11 contact-col">
                          <div class="form-group">
                            <input type="text" class="form-control" name="name" id="name">
                          </div>
                        </div>

                        <!-- Email Input -->
                        <div class="col-md-1 col-sm-1">
                          <div class="form-group">
                            <label class="contact-text">Email:</label>
                          </div>
                        </div>
                        <div class="col-md-11 col-sm-11 contact-col">
                          <div class="form-group">
                            <input type="email" class="form-control" name="email" id="email">
                          </div>
                        </div>

                        <!-- Message Input -->
                        <div class="col-md-1 col-sm-1">
                          <div class="form-group">
                            <label class="contact-text">Message:</label>
                          </div>
                        </div>
                        <div class="col-md-11 col-sm-11 contact-col">
                          <div class="form-group">
                            <textarea class="form-control" name="message" id="message" cols="30" rows="6"></textarea>
                          </div>
                        </div>
                      </div>
                  </div>
                </div>

                <!-- Submit Button -->
                <div class="col-md-8 col-sm-8 col-8 text-end">
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary">Send</button>
                    <div class="submitting"></div>
                  </div>
                </div>

              </div>
              </form>

PHP代码:

$errors = [];
  $errorMessage = '';

  if (!empty($_POST)) {
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];

      if (empty($name)) {
          $errors[] = 'Name is empty';
      }

      if (empty($email)) {
          $errors[] = 'Email is empty';
      } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $errors[] = 'Email is invalid';
      }

      if (empty($message)) {
          $errors[] = 'Message is empty';
      }

      if (empty($errors)) {
          $toEmail = 'cillinlyons@outlook.com';
          $emailSubject = 'New email from your contant form';
          $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];

          $bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
          $body = join('<br>', $bodyParagraphs);

          if (mail($toEmail, $emailSubject, $body, $headers)) {
              header('Location: https://cillinlyons.me');
          } else {
              $errorMessage = 'Oops, something went wrong. Please try again later';
          }
      } else {
          $allErrors = join('<br/>', $errors);
          $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
      }
  }

?>

您需要为您的按钮命名才能使用 php 进行设置 <button type="submit" class="btn btn-primary" name="submit">Send</button> 并使用 array_push() 将所有错误放入一个数组中, 我使用 isset() 来检查提交是否被点击。

 <?php

$errors = [];
$errorMessage = '';
if(isset($_POST['submit'])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (empty($name)) {
        array_push($errors, 'Name is empty');
    }

    if (empty($email)) {
        array_push($errors, 'Email is empty');
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        array_push($errors, 'Email is invalid');
    }

    if (empty($message)) {
        array_push($errors, 'Message is empty');
    }



if (empty($errors)) {
    $toEmail = 'cillinlyons@outlook.com';
    $emailSubject = 'New email from your contant form';
    $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];

    $bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
    $body = join('<br>', $bodyParagraphs);

    if (mail($toEmail, $emailSubject, $body, $headers)) {
        header('Location: https://cillinlyons.me');
    } else {
        $errorMessage = 'Oops, something went wrong. Please try again later';
    }
} else {
        foreach ($errors as $error)
            echo "<p style='color: red;'>{$error}</p>";
}}

我在 safari 上测试了这个及其工作