I am trying to get my HTML form to show that the data being collected has been successfully submitted to my Google Sheet. I am running an API on google sheets that receives my data after it has been submitted. The js code then runs a post. My issue is the js code opens a new tab upon submission and I don't want that. I added js to prevent that and provide a pop up but it will not display and just clears the form. how would I code this that instead of having a pop-up, the form changes to a submission complete message instead? Code snippet of my js script below. Google sites also requires all js, css, and HTML to be in one HTML file.
<script>
window.addEventListener("load", function() {
const form = document.getElementById('my-form');
form.addEventListener("submit", function(e) {
e.preventDefault();
const data = new FormData(form);
const action = e.target.action;
fetch(action, {
method: 'POST',
body: data,
})
.then(() => {
alert("Success!");
form.reset();
});
});
});
</script>
I tried redirecting it to a different webpage after submission but the site it redirects to never loads correctly. I have a feeling this is a google sites issue that I may just need to deal with until I create a better site. (lack of funds)