Skip to a Page on Google Forms - javascript

I'm programatically creating a Google Form using Google App Scripts right now, and my script automatically generates a page for each of the respondants that the form will be emailed out to.
Currently, the form has a dropdown at the beginning that the person uses to select their page, and then it forwards them to it, however, I'd prefer it if the email already sent out a pre-responded form that already started them on their page.
The reason I'm throwing them all into a single form is because I'm using Triggers to detect the form's submission, and since I have more than 20 people that require a form, that would break the trigger limit.
EDIT:
Example code:
the Form.getPublishedUrl() function returns the URL to respond the form, starting on the first page, however, I would like a URL to respond to the form starting on the 3rd or 4th page.
While the FormResponse.toPrefilledUrl() will allow me to autofill the correct response on the first page, it will not allow me to start the responder on the page that he would be forwarded to based on his pre-filled response on the first page.
tldr; :
How do I get a link to a specific page on a Google Form using Google App Scripts?

This should be a comment but don't have 50 rep yet...
Why do you need various HTML pages?
I think you should refactor your code so every form is on the same HTML and everything shows and hides with Jquery or Javascript. You can send the same URL but with differente URL parameters.
If this isn't your case, you should post some code of yours and detail a little bit more your question. Also, wich limit does it triggers?

Related

html script history.back() causes "Confirm Form Resubmission". How to suppress that browser prompt using my custom script? [duplicate]

I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.
I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.
Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)
Things I did so far : clear the browser cache.Code works fine but not the expected result.
Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.
Thanks in advance..
You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.
Your approach of clearing cache is correct. Coupled with that, you can use this approach.
<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>
One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.
When the user press the browser's back button, user can come to the
previous page where he submitted the details. And when the user submit
this, data is saved once more to the database.
According to how you described it, that is based on a doGet request. Which means every time you visit that URL, it will send the request with whatever parameters were added.
As someone already mentioned, if you switch the form to a post method and switch the Servlet to a doPost, you won't have this issue anymore.
Alternatively you can circumvent this with a javascript solution. Here are some options:
You can check if the user clicked the back button, disable form if true.
Another way is by storing a cookie which you check on page load, if it exists you can disable the form.
You can use this code also
$(document).ready(function() {
function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
You must use a Post-Redirect-Get pattern: https://en.m.wikipedia.org/wiki/Post/Redirect/Get.
Actually, every use of standard HTML forms with method="post" should be implemented with that pattern. It doesn't have any use for AJAX-posted forms, which actually could be another solution but will require more work and probably some architectural changes.
I had this same problem while building a django web app, and my solution was to not allow caching of the html that contains the form. In your request handler, do not allow the browser to cache the page. This will force the browser to get the page fresh from the document.
Which, in this case, you can just verify in your request handler if the requested form has already been submitted.
My code for reference:
from django.views.decorators.cache import never_cache
#never_cache
def GetForm(request, pk):
# Logic #
if (IsFormCompleted(pk)):
# Handle request #
Here is a solution.
give a random id in a hidden field on the form. Then on the server side, if the user resubmit, check if the random id already on the database. If so, redirect user.

Wordpress: Querying a database from within javascipt / jquery

I have a simple postcode search in my wordpress site. The user enters a postcode, clicks search and the browser directs to a new URL based on the postcode entered.
Presently, I have a large switch statement in the onclick handler of the button, which parses the postcode input in a text box and generates some display text and a URL which I then launch using window.open.
It works OK, but as I add more and more postcodes its bloating the code loaded into the browser and it's all visible in dev console. I'm also limited when it comes to making changes as I'm having to go in and modify the code to implement logic directly in my onclick handler. It's a bit of a mess!
What I would like to achieve is to store all of the relevant information in a mysql database, query the DB when the form button is clicked, and then launch the URL which will be returned by the db query (well, there'll be a bit more logic in the server script than that but ultimately I'll just be returning a URL and some text).
I'm struggling to get my head around how to implement this. It appears the DB queries need to be in PHP on the server and has to be queried asynchronously? I'm getting lost down a rabbit hole of AJAX which I'm struggling to understand!
I'm not looking to asynchronously update content on my page - at the point that we're running code in the onclick handler, we're ready to launch a new page based on the postcode entered. It appears the required solution is a little more complex than I hoped it might be.
Can anybody suggest how I might implement this? Do I have to use PHP & AJAX?
Thanks

How to delay confirmation page in Google Forms until script has run?

I’m working in modifying a google form to process information from my team. I want them to provide information on New Projects and Existing Projects. If Existing Projects is selected, it displays a URL to take the user to another form. I want the confirmation page to only display the link to the next form if the user has selected “Existing Projects”.
I know previous answers on this site and from Google Help state that you cannot dynamically change the confirmation notice. However, I have written a function which takes the user response and changes the confirmation response using GAS .setConfirmationMessage(). However, this only takes effect using the onSubmit trigger, and the confirmation page is loading too quickly right now to change the information from the current response.
function pageDisplay(){
var form=FormApp.getActiveForm();
var formResponses=form.getResponses();
var lastFormResponse=formResponses.pop();
var itemResponses=lastFormResponse.getItemResponses();
var itemResponse=[]; //initialize array
for (var i=0; i<itemResponses.length; i++){
itemResponse[i]=itemResponses[i].getResponse();
}
switch (itemResponse[0].toString()){
case "Existing Project":
form.setConfirmationMessage("Thank you now please visit URL");
form.setConfirmationMessage(url);
break;
case "New Project":
form.setConfirmationMessage("Thank you for your new information").setShowLinkToRespondAgain(true);
break;
}}
Question: How can I slow down the loading of the confirmation page until after the script has run? Is this possible?
I am getting used to GAS but have no experience in HTML or webpage JS so please be detailed if it involves those. Thank you for your help!
Google Forms doesn't include a built-in feature to change published form behaviors like delaying the displaying of the confirmation page.
One alternative is to get control of the POST request to the Google Form app by using UrlFetchApp but this could unnecessarily over-complicate your a solution to be deployed as "production". If you still want to take this way, you should the use HTMLService to display a html form with a custom form submit action that will emulate the form action of the published Google Form.

How to get bookmarklet running that allows to grab text, load a specific form and past the text into the form

I am very very new to JavaScript and I would like to build a Bookmarklet for a webservice I use. I need to grab text from any page, load the form of the service and post the text into the textfield of the form. So far I got this:
// grab text
javascript:(function(){var t=window.getSelection?window.getSelection().toString():document.selection.createRange().text;t="You selected: "+t;alert(t);})()
//load form
document.onload=function(){window.location=%22http://www.streetmails.com/index.php%3Fc=mailing%26a=content%22})();
//paste text into form
window.onload=function%20D(a,b){c=b.split('|');d=false;for(q=0;q<c.length;q++){if(c[q]==a)d=true;}return%20d;}function%20E(){f0=document.forms[0];f0['subject'].value='news from London';f0['content'].value='<echo$_GET['t']?>';f0[''].value='Add%20a%20picture...';f0['topicId'].value='4';}E
Can you help me? I searched and keept trying and trying but cant get it running....
Thanks in advance!
You can not do exactly what you are trying to do using a bookmarklet. When you execute a bookmarklet, you are executing Javascript inside the current web page. Javascript running in the web page of one web domain can not interact with pages from a different web domain. document.onload will almost never work in a bookmarklet because usually a web page is already loaded before you click the bookmarklet.
Lets call the page with text the "text-page". Lets call the page with the form the "form-page". A web form always submits to another page or to itself. It depends on the form's action value. Lets call that the action-page. Forms can be either submitted via GET or via POST.
To accomplish your goal with a bookmarklet, generally what is done is the bookmarklet will create a form on the text-page and then submit that form directly to the action-page.
If the action-page will accept a GET (only query string values), that is easiest. Look here for many such examples: https://www.squarefree.com/bookmarklets/search.html
If the action-page requires a POST, that is a little more complex because you must use Javascript to create the form: http://www.google.com/search?q=javascript%20create%20form
Addons, extensions, and user scripts can be used to do what you want more directly because they are not limited to operating only in the current domain, but of course they are more complicated to program.

Sharepoint: access subsite page contact from workflow

I'm trying to create a work flow that will send an email to the users in the contact field for the page that the initial link was followed from.
In other words, a user clicks a link on page ../top/sub/pages/page1.aspx which takes them to a form here: ..top/lists/feedback/newform.aspx. Once they submit the form on the top level page it starts a workflow (at ..top/lists/feedback/) which will email the users in the meta data for the referrer page (../top/sub/pages/page1.aspx) and finish by deleting the feedback item.
My problem lies in trying to email the correct user. I have tried to make a work-flow on the sub-site, but it seems like the work-flow has ZERO access outside of its directory.
My next idea would be to try and send the user as a parameter (as part of the form) using a script, but I'm unsure of how to access the information I need.
How would I access the page's contact user? Am I even on the right track?
ps. I dont have access to the server and therefore am unable to use visual studio
Probably there might be some dirty workaround.
add a hidden field to you top/lists/feedback/
add a delegate control that stores the contact on the page the user came from i.e. in the user session (or somewhere in SharePoint or in DB) and place the control on the pages ../top/sub/pages/ (or place it everywhere but make it work only on the pages)
add an itemeventreceiver in the list ..top/lists/feedback/ that grabs what the delegate control saved and inserts it into the hidden field in the item that is created in the ..top/lists/feedback/newform.aspx
use the hidden field in the wotkflow.
I hope you can either accept it or generate you own idea reading my answer.
Good luck!

Categories