简体   繁体   English

提交“联系”消息后,如何使它停留在页面上并生成文本确认?

[英]After submitting 'Contact' message, how do I get it to stay on page and generate a text confirmation?

I've seen this question asked on Stack Overflow before, but haven't been able to get the previous solutions to work, for my site! 我之前已经在Stack Overflow上看到过这个问题,但是对于我的网站,却无法使以前的解决方案正常工作!

I have a 'Contact' page on a website ( staging.twotailsmedia.com) . 我在网站( staging.twotailsmedia.com)上有一个“联系”页面。 And right now, the Contact page works and sends an email to the right place, but it generates a 'Thank you!' 现在,“联系方式”页面可以正常工作并将电子邮件发送到正确的位置,但是会产生“谢谢!” message on a new, separate, unformatted page. 新的,单独的,未格式化的页面上显示消息。 Not what I want. 不是我想要的 I want it to stay on the Contact page, generate the 'Thank you!' 我希望它保留在“联系人”页面上,生成“谢谢!” confirmation on the form next to the submit button. 在提交按钮旁边的表单上进行确认。

Here's my Form's HTML from the index.html file: 这是index.html文件中我表单的HTML

<form method="POST" action="mail.php">

   <div class="field half first">
      <label for="name">Name</label>
      <input type="text" name="name" id="name" />
   </div>

<div class="field half">
   <label for="email">Email</label>
   <input type="text" name="email" id="email" />
</div>

<div class="field">
   <label for="message">Message</label>
   <textarea name="message" id="message" rows="4"></textarea>
</div>

<ul class="actions">
   <li><input type="submit" value="Send Message" class="special" /></li>
   <li><input type="reset" value="Reset" /></li>
</ul>

</form>

Here's the PHP from the mail.php file: 这是来自mail.php文件的PHP

<?php

        $name = $_POST['name'];

        $email = $_POST['email'];

        $message = $_POST['message'];

        $formcontent="From: $name \n Message: $message";

        $recipient = "info@emailaddress.com";

        $subject = "A message from the Two Tails MEdia website!";

        $mailheader = "From: $email \r\n";

        mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

        echo "Thank You! We'll get back to you.";
}
?>

Usually I'd just get a Contact Form plugin from Wordpress but I'm trying to do a non-WP page so I can get a deeper understanding of the web development process. 通常,我只是从Wordpress获得一个Contact Form插件,但是我试图做一个非WP页面,以便对Web开发过程有更深入的了解。

Use Ajax to submit your form and by success show your message. 使用Ajax提交您的表单,并成功显示您的消息。 Try this code: 试试这个代码:

$('form').submit(function(e){
   e.preventDefault();
   $.ajax({
     type:'post',
     url:'mail.php',
     data:$(this).serialize(),
     success:function(){
         $('form')[0].reset(); // to reset the form
         $('#contact').hide(); // to hide the Contact article
         $('#message').show(); // to show the hidden message
     }
   })
})

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

相关问题 提交联系表后如何在同一页面获得成功信息? - How to get the sucess message in the same page after submitting the contact form? 提交联系表格后如何到达页面上的特定位置? - How do I get to a specific location on page after submitting contact form? 单击联系表单上的“提交”后,如何在wordpress中停留在同一页面上? - after clicking submit on a contact form, how do i stay on the same page in wordpress? 如何为我的联系表创建确认页面? - How do I create a confirmation page for my contact form? 提交电子邮件表格后,我希望停留在同一页面上并显示成功之类的消息 - After submitting email form, I want to stay on the same page and message like success to apear 使用PHP在页面上提交表单后如何返回消息? - How do I return a message after submitting a form on my page with PHP? 提交表单后如何在同一页面中获取成功消息? - How to get the success message in the same page after submitting the form? 提交表格,停留在同一页面上并确认消息 - submit form, stay on same page and message confirmation 提交表单后,如何在 ajax 错误消息中获取在服务器端处理的错误? - How do I get errors which are handled serverside in a ajax error message after submitting a form? 如何在Laravel中提交表单后重定向确认页面? - How to redirect confirmation page after submitting form in Laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM