0

I implemented a PRG pattern for form submit.

And on post form submit request. I redirect the page to custom defined rediret Page as given

<!DOCTYPE html>
<html>
   <head>
         <meta http-equiv = "refresh" content = "2; url =https://script.google.com/macros/s/AKfycbzSSFEHTowTqAqHabGhtipC0fsoSvaQWfV0uF34Igf2/dev?dest=saveClsTchRvwClsDtl &token=44" />

      <title>HTML Meta Tag</title>
      
   </head>
   <body>
      <p>This is demo redirect text.</p>
                    

   </body>
</html>

But when page reloads and I refresh the redirect Page again it again submit the post form submitted earlier.

Even when after redirect to when i generated the result page which got generated by GET request on refresh of the result page it submits post request for form submit.

Entire PRG pattern gets failed.

Below is the google App Script Code

var PostRoute = {};
PostRoute.path = function (route, callback) {
    PostRoute[route] = callback;
};

PostRoute.path("saveClsStdDtl", saveClsStudentDetails);

function doPost(e) {
    try {

        Logger.log("POST request");
        Logger.log(e);

        if (PostRoute[e.parameter.dest]) {
            PostRoute[e.parameter.dest](e);
        } else {
            return homePage(e);
        }

        return redirect(e);

    } catch (err) {
        Logger.log(err);
    }
}

function doGet(e) {

    if (e.parameter.token != null) {
        receivedTokenValue(e.parameter.token);
    }
    var token = generateTokenValue(e);
    e.parameter.token = token;
    e.parameters.token = [token];
    Logger.log(e);

    Route.path("clsDtl", tchSubmitClassDetails);
    Route.path("saveClsTchRvwClsDtl", homePage);

    if (Route[e.parameter.dest]) {
        return Route[e.parameter.dest](e);
    } else {
        return homePage(e);
    }

}

function redirect(e) {
    Logger.log("redirect    : " + e.parameter);
    return renderPage("RedirectPage", {});
}

function renderPage(filename, varObjects) {

    var tmp = HtmlService.createTemplateFromFile(filename);
    var keys = Object.keys(varObjects);

    if (varObjects) {
        keys.forEach(function (key) {

            Logger.log(key);
            Logger.log(varObjects[key]);
            tmp[key] = varObjects[key];

        })
    }
    return tmp.evaluate();

}

function saveClsTchRvwStudentDetails(e) {
    Logger.log("Inside saveClsTchRvwStudentDetails");
}
sharad
  • 1
  • 2
  • Can you provide more information about your issue? Also, can you share the Apps Script code as well? @sharad – ale13 Sep 15 '20 at 13:17
  • @ale13 Please have a look I have added the Google App script Code. Do ask for any other clarification. – sharad Sep 15 '20 at 19:08
  • When you say the post do you mean an actual POST request? I'm failing to see what is your expected output, would you please let that be clear, so someone like me that doesn't fully understand the PRG pattern can have a shot at helping you? – Raserhin Sep 18 '20 at 08:01
  • yes by post means actually I meant POST request to server and when you refresh it resubmits the POST request and to avoid the double submission we implement POST REDIRECT GET (PRG). For more details might this link help you to understand fast the theory https://en.wikipedia.org/wiki/Post/Redirect/Get#:~:text=Post%2FRedirect%2FGet%20(PRG,submitting%20the%20form%20another%20time. @Raserhin – sharad Sep 22 '20 at 20:09
  • So essentially you don't want the form to resubmit when you refresh the page, am I correct? @sharad – ale13 Sep 24 '20 at 08:46
  • Yes @ale this is the whole purpose of PRG pattern and PRG is the server side binding towards the client (web browser) hope you understand what I am saying – sharad Sep 24 '20 at 20:01
  • Have you looked into this [here](https://stackoverflow.com/questions/50919151/redirect-after-processing-a-post-request-in-apps-script)? – ale13 Oct 01 '20 at 07:28

0 Answers0