简体   繁体   English

如何在不刷新整个页面的情况下重新加载此表单?

[英]How to reload this form without refreshing whole page?

I am using "Send Email from a Static HTML Form using Google Apps Mail" on my static site.我正在使用“使用 Google Apps 邮件从 Static HTML 表单发送 Email”在我的 ZA81259CEF8E959C624ZDF1D466 站点上。 But when I submit a form i have to refresh the whole page to submit another mail.但是当我提交表单时,我必须刷新整个页面才能提交另一封邮件。 If I don't reload the page success text don't vanish and send button don't work.如果我不重新加载页面成功文本不会消失并且发送按钮不起作用。 So i am asking is there any way to refresh my form section without refreshing the whole page?所以我问有没有办法在不刷新整个页面的情况下刷新我的表单部分? please help me how to do it.请帮我怎么做。

 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> <div class="container mail text-center justify-content-center mt-5"> <div class="row"> <div class="col-12"> <h3>Contact Me Now:</h3> <hr> </div> <div class="col-md-12 col-xs-12 form"> <form method="post" role="form" action="https.//script.google:com/macros/s/AKfycbwtLbTgUDGQxi9FY8Jks6bJs3TnYPBNU7rvO8b8_zrdyD4Pa6g/exec" method="post" role="form" class="gform " data-email=""> <div class="form-row"> <div class="col form-group"> <input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4 " data-msg="Please enter at least 4 chars " required="true" /> </div> <div class="col form-group"> <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email " data-msg="Please enter a valid email " required="true" /> </div> </div> <div class="form-group"> <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4 " data-msg="Please enter at least 8 chars of subject " required="true" /> </div> <div class="form-group"> <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for me " placeholder="Message " required="true"></textarea> </div> <div class="text-center"> <button type="submit" class="btn btn-success w-100 submit">Send Message</button> </div> <div style="display:none" class="thankyou_message"> <div class="alert" role="alert"> <em>Thanks</em> for contacting me. I will get back to you soon.<br> <i class="fas fa-sync fa-spin"></i> </div> </div> </form> </div> </div> </div> <script data-cfasync="false" type="text/javascript" src="https.//cdn.rawgit.com/dwyl/html-form-send-email-via-google-script-without-server/master/form-submission-handler.js"></script>

If the form is refreshing the page, you'll need to use preventDefault to cancel its default behaviour then use reset() to reset the form.如果表单正在刷新页面,您需要使用preventDefault取消其默认行为,然后使用reset()重置表单。

 const form = document.querySelector('form'); form.addEventListener('submit', e => { e.preventDefault(); [...form.elements].forEach(input => console.log(`${input.name}: ${input.value}`)); // Not Important form.reset(); });
 <form method="POST"> <input type="text" name="name" placeholder="name"> <input type="password" name="password" placeholder="password"> <button type="submit">Submit</button> </form>

In JavaScript there is a function for forms which will reset the form clearing all the data in it, ready for another submit.在 JavaScript 中有一个 function 用于 forms 它将重置表单以清除其中的所有数据,准备再次提交。 This can be accomplished by running a JavaScript function on submit.这可以通过在提交时运行 JavaScript function 来完成。 This requires a couple changes in your code.这需要对您的代码进行一些更改。

Firstly, we need to change this:首先,我们需要改变这一点:

<button type="submit" class="btn btn-success w-100 submit">Send Message</button>

to this:对此:

<button type="button" onclick="submitForm1()" class="btn btn-success w-100 submit">Send Message</button>

Once done, we can add some JavaScript to your page.完成后,我们可以将一些 JavaScript 添加到您的页面。 If you have a JavaScript file linked to the page already, you can just add this code to that.如果您已经有一个链接到该页面的 JavaScript 文件,您只需添加此代码即可。

function submitForm1() {
    let form = document.getElementsByClassName("gform");
    form.submit();
    form.reset();
}

You can also use document.getElementById("[id]");您也可以使用document.getElementById("[id]"); and add an ID to your form.并在您的表单中添加一个 ID。 This is also preferable.这也是优选的。

The first thing that comes to mind it's do all this on js so you can through ajax request send what you want.想到的第一件事就是在js上做这一切,所以你可以通过ajax请求发送你想要的。 But I think it's not what you're looking for.但我认为这不是你要找的。 You can't send data from page without refreshing, that's how it's work, php or html with some functional.你不能在不刷新的情况下从页面发送数据,这就是它的工作原理, phphtml具有一些功能。 You can change...你可以改变...

<button type="button" class="btn btn-success w-100">Send Message</button>

... and collect all data by JavaScript and send it through ajax. ...并通过 JavaScript 收集所有数据并通过 ajax 发送。

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

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