简体   繁体   English

修改 Formspree 代码以将联系人响应发送到 email - HTML/CSS/JS

[英]Modifying Formspree code to send contact responses to email - HTML/CSS/JS

I have the following form:我有以下表格:

 /* Contact Form */ input[type=text], [type=email], select, textarea { width: 100%; padding: 12px; border: 1px solid #555; margin-top: 6px; margin-bottom: 16px; resize: vertical; } input[type=submit] { background-color: #0563bb; color: white; padding: 12px 20px; border: none; cursor: pointer; } input[type=submit]:hover { opacity: 0.9; }.contactform { position: relative; border-radius: 50px; background-color: #f2f2f2; padding: 5px; z-index: 2; display: block; margin-left: auto; margin-right: auto; margin-bottom: auto; margin-top: 1%; width: 100%; animation-name: gradient; animation-duration: 3s; animation-iteration-count: infinite; }.contactform:hover { animation-name: gradient; animation-duration: 15s; animation-iteration-count: infinite; }.column { float: center; width: 50%; margin-top: 6px; padding: 20px; display: block; margin-left: auto; margin-right: auto; }.row:after { content: ""; display: table; clear: both; } @media screen and (max-width: 600px) {.column, input[type=submit] { width: auto; margin-top: 0; } } @keyframes shake { 10%, 90% { transform: translate3d(-1px, 0, 0); } 20%, 80% { transform: translate3d(2px, 0, 0); } 30%, 50%, 70% { transform: translate3d(-4px, 0, 0); } 40%, 60% { transform: translate3d(4px, 0, 0); } }
 <section id="contact"> <div class="container" data-aos="fade-up"> <div class="contactform"> <div style="text-align:center"> <div class="section-title"> <h2><br/>Get In Touch</h2> </div> <p>Feel Free To Reach Out To Me Through This Form: </p> </div> <div class="row"> <div class="column"> <form name="myform" action="https.//formspree.io/f/xdobkgny" method="POST" novalidate> <label for="firstname">First Name</label> <input type="text" id="first name" name="firstname" placeholder="Your First Name.." required> <label for="lastname">Last Name</label> <input type="text" id="lastname" name="lastname" placeholder="Your Last Name.:" required> <label for="email">Email.</label> <input type="email" id="email" name="email" placeholder="Your Email.." required> <label for="subject">Subject</label> <textarea id="subject" name="subject" placeholder="Lets Collaborate.:" style="height:170px" required></textarea> <input type="submit" value="Submit"> </form> </div> </div> </div> </div> </section>

This form is sending responses to the desired email, however I want to change the redirection page to thankyou.html instead of the Formspree redirection page.此表单正在向所需的 email 发送响应,但是我想将重定向页面更改为thankyou.html而不是 Formspree 重定向页面。 How would I modify this following JS code?我将如何修改以下 JS 代码? This JS code also has some other information such as error messages...etc but I already got that covered so can I just remove that part?此 JS 代码还有一些其他信息,例如错误消息......等等,但我已经了解了,所以我可以删除那部分吗? Essentially, the only thing I want to change is redirection on the submission and dont want any errors or any other info.本质上,我唯一要更改的是提交时的重定向,并且不想要任何错误或任何其他信息。 The only reason I am using the JS code provided by Formspree is to change the redirection and nothing else.我使用 Formspree 提供的 JS 代码的唯一原因是更改重定向,仅此而已。 How would this be possible?这怎么可能? I want to remove text such as Thanks for your submission!我想删除诸如Thanks for your submission! and Oops...... which is provided in the following JS code since I already have that covered with my own js code in my file.Oops......这是在以下 JS 代码中提供的,因为我已经在我的文件中包含了我自己的 js 代码。

JS code: JS代码:

var form = document.getElementById("my-form");
    
    async function handleSubmit(event) {
      event.preventDefault();
      var status = document.getElementById("my-form-status");
      var data = new FormData(event.target);
      fetch(event.target.action, {
        method: form.method,
        body: data,
        headers: {
            'Accept': 'application/json'
        }
      }).then(response => {
        status.innerHTML = "Thanks for your submission!";
        form.reset()
      }).catch(error => {
        status.innerHTML = "Oops! There was a problem submitting your form"
      });
    }
    form.addEventListener("submit", handleSubmit)

Update (After Pufferfish Answer)更新(河豚回答后)

https://watch.screencastify.com/v/BSkuGitl5ACHDWEvGziM https://watch.screencastify.com/v/BSkuGitl5ACHDWEvGziM

To redirect using JavaScript, you can use window.location = <URL> .要使用 JavaScript 进行重定向,您可以使用window.location = <URL> If you want this to happen after the fetch has happened, no matter, the outcome, you can pass a function to the .finally() method:如果您希望在fetch发生后发生这种情况,无论结果如何,您都可以将 function 传递给.finally()方法:

var form = document.getElementById("my-form");

async function handleSubmit(event) {
      event.preventDefault();
      var data = new FormData(event.target);
      fetch(event.target.action, {
        method: form.method,
        body: data,
        headers: {
            'Accept': 'application/json'
        }
      }).finally(() => {
        window.location = "/thankyou.html";
      });
}
form.addEventListener("submit", handleSubmit)

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

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