简体   繁体   English

提交 html 表单时显示成功消息

[英]Success Message display on submit html form

i am trying to display success message just above submit button but its directing me to form page.Here is what i am trying to do-我正在尝试在提交按钮上方显示成功消息,但它引导我进入表单页面。这就是我想要做的 -

//form is on deck.html page
<div class="row footer-newsletter justify-content-center">
      <div class="col-lg-6">
        <form action="forms/subscribe.php" method="post">
          <input type="email" name="email" placeholder="Enter your Email"><input type="submit" value="Subscribe">
        </form>
      </div>

i am getting form inputs on my email so where i am wrong in below code我在我的 email 上获得表单输入,所以我在下面的代码中出错了

// 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);  
?><!-- include your own success html here.
So i am getting form details to email but succes message below is appearing on /forms/subscribe.php 
where actually i am looking to display just above button   -->
Thank you for contacting us. We will be in touch with you very soon.
<?php

i tried on submit pop up,alert box but it always took me to subscribe page and i am looking to display just before subscribe button我尝试提交弹出窗口,警告框,但它总是让我订阅页面,我希望在订阅按钮之前显示这是图像

What we need to do is:我们需要做的是:

  1. Capture and stop the submit event from continuing捕获并阻止提交事件继续
  2. Show the message显示消息
  3. After some time (let's say 2 seconds), continue the submission.一段时间后(比如说 2 秒),继续提交。

Check the code below:检查下面的代码:

We attach an event listener to the form.我们将事件侦听器附加到表单。 When the 'submit' event happens, we want to execute our code.当“提交”事件发生时,我们想要执行我们的代码。 The e variable represents the event. e变量代表事件。 We want to stop the event from doing its normal stuff (in this case, send the user to another page) so we say e.preventDefault() .我们想阻止事件做正常的事情(在这种情况下,将用户发送到另一个页面),所以我们说e.preventDefault()

I created a p element with the message on top of the form.我在表单顶部创建了一个带有消息的p元素。 This element is hidden by default, but if we add a class show to it, it appears.这个元素默认是隐藏的,但是如果我们给它添加一个 class show ,它就会出现。 So, that's what we do.所以,这就是我们所做的。 After preventing the event from continuing, we add this class to the message.在阻止事件继续后,我们将这个 class 添加到消息中。

Finally, we use the setTimeout() function to count to 2 seconds (2000 ms) and then execute form.submit() , which sends the user to the subscribe page.最后,我们使用setTimeout() function 计数到 2 秒(2000 毫秒),然后执行form.submit() ,将用户发送到订阅页面。

 const form = document.querySelector('form'); const thankYouMessage = document.querySelector('#thank-you-message'); form.addEventListener('submit', (e) => { e.preventDefault(); thankYouMessage.classList.add('show'); setTimeout(() => form.submit(), 2000); });
 #thank-you-message { display: none; } #thank-you-message.show { display: block; }
 <p id="thank-you-message"> Thank you for contacting us. We will be in touch with you very soon. </p> <form action="forms/subscribe.php" method="post"> <input type="email" name="email" placeholder="Enter your Email"> <input type="submit" value="Subscribe"> </form>

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

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