Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting from doPost() in Google Web App, HTML form with App script

Say I have an HTML file with various inputs. According to these inputs I would like to send an email and then display a thank you message or redirect to a thank you page.

I am loading the index.html file in the doGet() as below:

function doGet(){
    var template = HtmlService.createTemplateFromFile('index');
    template.action = ScriptApp.getService().getUrl();
    return template.evaluate();
}

After adding the implementation which I require in doPost(e) function, deploying the code into a web app, filling in the form and submitting, everything seems to be working perfectly apart from the last bit. I want to show an output message or redirect to a thank you page, or something of the sort, but all I am seeing is a blank page, where there would have been the HTML form.

I have tried:

function doPost(e) {
    //all logic
    return ContentService.createTextOutput("Thank you");
}

And..

function doPost(e) {
    //all logic
    return ContentService.createTextOutput(JSON.stringify(e.parameter));
}

And..

function doPost(e) {
    //all logic
    var htmlBody = "<h1>Thank you.</h1>";
    return HtmlService.createHtmlOutput(htmlBody);
}

And..

function doPost(e) {
    //all logic
    var template = HtmlService.createTemplateFromFile('Thanks');
    return template.evaluate();
}

In all the listed cases the HTML form simply seems to disappear and I can only see a blank screen.

Any ideas?

like image 914
Jurgen Cuschieri Avatar asked Mar 07 '23 09:03

Jurgen Cuschieri


1 Answers

In GAS, sending a POST request via web app is not as simple as it seems. Go ahead and try a little experiment: add the following JS code to the HTML page served by the doGet() function just before the closing <body> tag:

... 
<script>
console.log(window.location);
</script>
</body>

When inspecting the output in the console, you'll likely get something like:

enter image description here

The issue is that the web app URL ending with '/exec' doesn't actually link to self. Ratner, the googleusercontent.com link is the URL you'll be redirected to when opening links and forms. However, in order to get the form submit to work, the POST request should be sent to the "published" URL of your web app (the one ending with '/exec').

You must therefore override the defaults by implementing your own routing. There are many ways to achieve this result - here's the easiest one:

    <form method="post" action="<?!= ScriptApp.getService().getUrl() ?>">
    <input type="submit" value="submit">
    </form>

If the <?!= ?> notation looks unfamiliar to you, please check the section on scriptlets and HTML templates in GAS https://developers.google.com/apps-script/guides/html/templates

Basically, what happens is that the web app URL is force-printed to the template before raw HTML is sent to your browser for rendering. This makes sure you'll hit the correct URL when 'submit' event occurs.

like image 73
Anton Dementiev Avatar answered Apr 26 '23 11:04

Anton Dementiev