DEV Community

Discussion on: Submit a Form to a Google Spreadsheet

Collapse
omerlahav profile image
Omer Lahav Author

Hi there mauroian, glad it helped!
About the redirecting part: How do you decide whether the user is being sent to the success/error page? Is it based on whether the form was sent successfully to the spreadsheet, by one of the form's fields or by another criterion?

Collapse
mauroian profile image
mauroian • Edited on

All I need is that the form was sent successfully to the spreadsheet. In which case, I'd like the user to be redirected to a success page :)

Thread Thread
omerlahav profile image
Omer Lahav Author • Edited on

Try to find this line on the current code:

.then(response => console.log('Success!', response))
Enter fullscreen mode Exit fullscreen mode

And try to replace it with (You can pick either one of the options with comments, no need for both of them):

.then((response) => {
    console.log('Success!', response);
    // similar behavior as an HTTP redirect
    window.location.replace("https://dev.to/");

    // similar behavior as clicking on a link
    window.location.href = "https://dev.to/";
});
Enter fullscreen mode Exit fullscreen mode

Same goes for the error part, it should do the trick.

Thread Thread
mauroian profile image
mauroian

Done that. Now nothing gets submitted to the spreadsheet (and no redirect or anything else happens).

Thread Thread
mauroian profile image
mauroian

By the way, I think this might be important: in the original code, which was working, when I successfully submitted data to the spreadsheet, my HTML page did not change at all. It continued to show the input fields and submit button.

Basically, the line:
.then(response => console.log('Success!', response))

did nothing, as no 'Success!' message was shown in my page.

So, the issue could be in how/what the google script sends as response, or in the original code in index.html itself.

I am stuck at the moment... would be so happy if this worked!

Thread Thread
omerlahav profile image
Omer Lahav Author

It wasn't meant to do anything else besides sending to the spreadsheet.
In the following line:

form.addEventListener('submit', e => {
.............
}
Enter fullscreen mode Exit fullscreen mode

Everything between the { .... } will happen after the form is being submitted and you can modify it as much as you want, anything that happens there is up to your limits and not really relevant to this article or this mechanism :)
This whole article was only about the "sending-to-spreadsheet" part and nothing else, this is why nothing is happening other than that.
It already feels more of a tech support session than an easy js fix so just feel free to ping me wherever you'd like through one of the contact channels in my profile. The redirecting part is pure JS and can be found online but I'll try to help you out.