提交表单后如何显示自定义消息?

how do i display a custom message after submitting a form?

我有一个 html 表单,在提交后使用 phpMailer 发送电子邮件。一切正常,但我想在发送电子邮件时在表单 上显示一条消息 。 使用我的代码,一条消息显示在空白页面上。

我的php代码:

<?php

$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$msg = $_POST['msg'] ?? '';

  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\SMTP;
  use PHPMailer\PHPMailer\Exception;

  require 'vendor/autoload.php';

  $mail = new PHPMailer(true);

  try {

    $mail->Host='smtp.gmail.com';
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Port=587;
    $mail->SMTPAuth=true;
    $mail->SMTPSecure='tls';
    $mail->Username='email@email.com';
    $mail->Password='password';

    $mail->setFrom($email, $name);  // who send the email
    $mail->addAddress('email@email.com'); // who recive the email

    $mail->isHTML(true);
    $mail->Subject = 'Portfolio response';
    $mail->Body = $msg;

    $mail->send();
     echo 'Your message has been sent!';
    } catch (Exception $e){
        echo '';
    }

?>

您的消息已发送!显然是显示的消息。

Html 表单代码:

 <form action="mail-handler.php" method="POST" id="contact-form">
              <span class="contacts-text" id="first-contacts-text">To get in touch with me, please compile this form.</span>
              <span class="contacts-text">I will reply as soon as possible. Thank you!</span>
              <ul class="form-content">
                  <li class="form-input">
                      <label for="name" class="label">Full name</label>
                      <input class="input" id="name" type="text" name="name" required>
                  </li>
                  <li class="form-input">
                      <label for="email" class="label">E-mail</label>
                      <input class="input" id="mail" type="text" name="email" required>
                  </li>
                  <li class="form-input">
                      <label for="msg" class="label">Insert text</label>
                      <textarea class="input" id="comment" type="text" name="msg" cols="30" rows="10" style="resize: none;" required></textarea>
                  </li>
                  <li class="form-input" id="last-input">
                      <input class="input" id="form-button" type="submit" value="submit" name="submit" onclick="sendEmail()">
                  </li>
              </ul>
            </form>

多谢指教!

首先,将 index.html 的名称更改为 index.php,以便 PHP 解释器可以解析它。

接下来修改你的成功声明正文,即:

try {
    $mail->send();
    header('Location: index.php?mailsent=1');
    die(); // we don't want exactly anything after sending redirect header.
} catch (Exception $e){
    echo '';
}

如果 $_GET['mailsent'] 变量可用,最后在表单文件中添加消息。

HTML code of your page...
...
<?php
    if (isset($_GET['mailsent']) && intval($_GET['mailsent']) == 1 ){
        echo '<div class="messagesbox">Mail was sent</div>';
    }
?>
<form action="mail-handler.php" method="POST" id="contact-form">
   ... your form body skipped here
</form>

其他选项

如果不想通过GET数组传递参数,可以尝试使用session。

此外,您可以只创建一个静态 thankyou.html 页面并在邮件提交后将用户重定向到那里

此外,如果您将表单提交的整个 PHP 代码移动到 index.php 中,您将不需要进行重定向。

最后,正如您添加的那样 - 虽然它是 一页 网站,但您还应该考虑使用 AJAX 可能与 jQuery - 这样你将能够在不离开页面的情况下提交表单,在不重新加载的情况下显示消息等等。无论如何,这个主题肯定太宽泛了,无法在这个答案中描述,我只能建议你熟悉 jQuery AJAX.