Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form redirect request #17

Open
siferiandude12 opened this issue Oct 25, 2022 · 8 comments
Open

Form redirect request #17

siferiandude12 opened this issue Oct 25, 2022 · 8 comments

Comments

@siferiandude12
Copy link

This is a fantastic overview, and it works great. The only issue I have is finding a way to redirect the page to another URL after submission.

It returns a success page, however it does not redirect.

image

What code would I need to add inside of this Javascript to redirect the user, instead of them seeing the message above?

Thanks!

@iharpe18
Copy link

Same. I tried adding:

finally { lock.releaseLock() window.location.href = "http://google.com"; }

But since this script is firing externally from google scripts "window" is undefined.

@Maheshcheegiti
Copy link

Same Problem

@iharpe18
Copy link

iharpe18 commented Nov 1, 2022

I fixed it by adding this to the html and not the google script:

<script type="text/javascript"> window.addEventListener("DOMContentLoaded", function() { const yourForm = document.getElementById('SurveyForm'); yourForm.addEventListener("submit", function(e) { e.preventDefault(); const data = new FormData(yourForm); const action = e.target.action; fetch(action, { method: 'POST', body: data, }) .then(() => { window.location.replace('https://www.WEBSITE.com/thank-you') }) }) }); </script>

@siferiandude12
Copy link
Author

I fixed it by adding this to the html and not the google script:

Where did you add that in the HTML? Was it inside the form action, or outside the form action brackets?

@iharpe18
Copy link

iharpe18 commented Nov 2, 2022

I put it after the input type = submit outside of the form.

@ashloren
Copy link

ashloren commented Nov 6, 2022

In case anyone wants to add dropdown selects, it worked fine for me.

Here is my form:
https://abortourcourt.com/responsive-form

And the spreadsheet:
https://docs.google.com/spreadsheets/d/1PTdsv6qT4KnhWWfac0OhjH_KC7deNH_uIuWw5_seAwA/edit?usp=sharing

@trjjnhrs
Copy link

trjjnhrs commented Mar 18, 2023

I fixed it by adding this to the html and not the google script:

<script type="text/javascript"> window.addEventListener("DOMContentLoaded", function() { const yourForm = document.getElementById('SurveyForm'); yourForm.addEventListener("submit", function(e) { e.preventDefault(); const data = new FormData(yourForm); const action = e.target.action; fetch(action, { method: 'POST', body: data, }) .then(() => { window.location.replace('https://www.WEBSITE.com/thank-you') }) }) }); </script>

This worked. For those wondering how to implement this... Literally copy all of this and paste it at the bottom of your HTML document that has your form. Inside your <form></form>, add the following:

<form id="SurveyForm"></form>

@roboes
Copy link

roboes commented Mar 20, 2023

I fixed it by adding this to the html and not the google script:
<script type="text/javascript"> window.addEventListener("DOMContentLoaded", function() { const yourForm = document.getElementById('SurveyForm'); yourForm.addEventListener("submit", function(e) { e.preventDefault(); const data = new FormData(yourForm); const action = e.target.action; fetch(action, { method: 'POST', body: data, }) .then(() => { window.location.replace('https://www.WEBSITE.com/thank-you') }) }) }); </script>

This worked. For those wondering how to implement this... Literally copy all of this and paste it at the bottom of your HTML document that has your form. Inside your <form></form>, add the following:

<form id="SurveyForm"></form>

It also worked for me. Here's the full code (without the Google Apps script URL):

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>


<body>

<form 
  id="SurveyForm"
  method="POST" 
  action="YOUR_GOOGLE_SCRIPT_URL"
>
  <input name="Email" type="email" placeholder="Email" required>
  <input name="Name" type="text" placeholder="Name" required>
  <button type="submit">Send</button>
</form>


<script type="text/javascript">

window.addEventListener("DOMContentLoaded", function() { const yourForm = document.getElementById('SurveyForm'); yourForm.addEventListener("submit", function(e) { e.preventDefault(); const data = new FormData(yourForm); const action = e.target.action; fetch(action, { method: 'POST', body: data, }) .then(() => { window.location.replace('https://www.WEBSITE.com/thank-you') }) }) }); 

</script>


</body>

</html>

Update (June 11, 2023): This trick is not working anymore. I am now using SheetDB service. Here is a working code example:

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>


<body>

<form 
  id="SurveyForm"
  method="POST" 
  action="YOUR_SHEETDB_SCRIPT_URL"
>
  <input id="Date" name="Date" type="hidden" readonly="readonly">
  <input name="Email" type="email" placeholder="Email" required>
  <input name="Name" type="text" placeholder="Name" required>
  <button type="submit">Send</button>
</form>


<script>

// Get current Date
var date_today = new Date().toISOString().slice(0, 10)
document.getElementById("Date").value = date_today.toString();

var form = document.getElementById('SurveyForm');
form.addEventListener("submit", e => {
e.preventDefault();
fetch(form.action, {
	method : "POST",
	body: new FormData(document.getElementById("SurveyForm")),
}).then(
	response => response.json()
).then((html) => {
  // you can put any JS code here
  window.location.replace('https://www.WEBSITE.com/thank-you')
});
});
</script>


</body>

</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants