简体   繁体   English

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

[英]how do i display a custom message after submitting a form?

I have a html form that sends email, using phpMailer, after its submission.我有一个 html 表单,它在提交后使用 phpMailer 发送 email。 Everything works fine but i want to display a message on the form when the email is sent.一切正常,但我想在发送 email 时在表单上显示一条消息。 With my code, a message is displayed on an empty white page.使用我的代码,一条消息显示在空白页上。

My php code:我的 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 '';
    }

?>

Your message has been sent!您的留言已发送! is obviously the message displayed.显然是显示的消息。

Html form code: 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>

Thanks in advice!谢谢指教!

First, change the name of your index.html to index.php , so it can be parsed by a PHP interpreter.首先,将index.html的名称更改为index.php ,这样它就可以被 PHP 解释器解析。

Next modiy your success statement body, ie:接下来修改您的成功声明正文,即:

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

Finally in file with your form add the message if $_GET['mailsent'] variable is available.如果$_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>

Other options其他选项

If you don't want to pass arguments via GET array, you can try to use sessions.如果不想通过 GET 数组传递 arguments,可以尝试使用会话。

Also, you can just create a static thankyou.html page and redirect the user there, after mail submitting此外,您可以创建一个thankyou.html页面并在提交邮件后将用户重定向到那里

Also, if you move the whole PHP code for the form submittion into index.php you won't need to make redirects.此外,如果将表单提交的整个 PHP 代码移动到index.php中,则无需进行重定向。

Finally, as you added - while it's one-page site you should also consider using AJAX probably with jQuery - that way you're will be able to submit the form without leaving the page, display messages without reloading, etc. Anyway, this topic is definitely too broad to be described in this answer and I can only suggest you get familiar with jQuery AJAX .最后,正如您添加的那样 - 虽然它是单页网站,但您还应该考虑使用 AJAX 可能与 jQuery - 这样您就可以在不离开页面的情况下提交表单,在不重新加载的情况下显示消息等。无论如何,这个主题绝对过于宽泛,无法在此答案中进行描述,我只能建议您熟悉jQuery AJAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM