0
votes

I created a web app form using Google Apps Script and the HTMLService.

It is a one-page form with a submit button at the bottom.

When submitted, the form validates whether the data input into the form is valid, and if valid, it logs the form data to a spreadsheet.

That all works so far.

I now need the user to be sent to a confirmation page, and the confirmation page needs to be able to have parameters passed to it (to display certain information on the confirmation page).

main.gs:

function doGet(e) {

  var template = HtmlService.createTemplateFromFile('form');
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

function processFormSubmission(formData) {

  Logger.log('starting processPoRequest');
  Logger.log('po: ' + JSON.stringify(formData, null, 2));
  // code for appending data to sheet here

}

form.html:

<!DOCTYPE html>

<form id="form1" name="form1">

  <label for="info" id="info_label">Info</label>
  <input id="info" name="info" type="text">

  <input class="btn" id="button" onclick="onClickFunctions(document.getElementById('form1'))" type="submit" value="Submit">

</form> 

<script>

function onClickFunctions(formData) {

  console.log('starting onClickFunctions');

  var allDataValid = validateForm(formData);

  if (allDataValid === true) {

    google.script.run.withSuccessHandler().processFormSubmission(formData);

  }

}

function validateForm(form) {

  console.log('starting validateForm');

  var errors = 0;

  var element = document.getElementById('info');
  if (!form.info) { element.classList.add("validation_error"); errors++; if (errors === 1) element.focus(); }
  else element.classList.remove("validation_error");

  if (errors > 0) return false;
  else            return true;

}

</script>

confirmation.html:

<!DOCTYPE html>

<?!= confirmationMessage ?>

I don't know what to put in .withSuccessHandler() to make it so that the user is brought to the confirmation page.

I've Googled this extensively and found these results on Stack Overflow, and each one suggests a different solution, but none of them actually include complete working code for a solution:

Possible solutions using doPost:

Send form by email and track responses in spreadsheet

HtmlService doPost With Google Docs Form

HtmlService doPost

I messed around with doPost but I couldn't figure out how to get it to be invoked, and I couldn't find any official documentation in the HTMLService docs.

Possible solution using the link to the web app in an a href:

href in HtmlService

If my button was a link that looked like a button, I'm not sure how I would execute the form validation function when the link is clicked.

1
you are confusing a webapp with a google form. you are not using POST. you are just calling a server callback. from successhandler just show a "continue" link.Zig Mandel
Hey Zig, it's not a Google Form, it's an HTML form created in Google Apps Script and served using the "Deploy as Web App" functionality. When Googling this kind of scenario I see a lot of people talking about doPost but I just couldn't figure out how to use it right. Anyway, I like the idea of displaying a continue link. That, combined with what Bjorn suggested below might be the best way to do this. When the user clicks submit, the submit button is temporarily disabled, and then if the form passes validation, I change the button to have a green check with "Submitted" so they know it wentEmployee
...through, then maybe display a link to submit the form again. I will try this now and see if I can get something working.Employee
that's exactly what im saying. its not a google form. your researched links are about google forms seems.Zig Mandel
OH. You're right! I totally didn't even realize that. doPost() is probably just for scripting Google Forms.Employee

1 Answers

0
votes

I have done this two different ways.

  1. had a hidden statement that gets shown, and the form gets hidden.

or

  1. use .withSuccessHandler(google.script.host.close()), but have the processFormSubmission function open a new dialogue.