SOLVED

Re: Pulling Field from One Form to Another

Go to solution
Samantha_Cossum
Level 3

Pulling Field from One Form to Another

I am working on a semi-complicated application for our business and have gotten stuck and hoping the Nation can help! 

The desire is to have applicants fill out part one of the form and then when they submit it, it calculates their current collections. I have this part working and the math is firing correctly. 

The part I'm struggling with is on the next section, they are supposed to fill out a target but some of the info needed they just filled out on the page before so I don't want to ask it to them again but I don't know how to get the form to do the calculations if the data isn't in the form. 

Here is the first form: https://go.schedulinginstitute.com/RCBC-App-Target-Collection.html 

Here is the second form: https://go.schedulinginstitute.com/RCBC-App-Testing-2.html 

There's examples on the pages of the math we are trying to do so hopefully that will help if my explanation was unclear. 

The only idea I've had is if I could store the values from the first form in a cookie and then have hidden fields that are populated by the cookie but I couldn't work on how to pull information into a cookie on a form fill. 

Any suggestions would be much appreciated! Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Pulling Field from One Form to Another

Certainly a couple of ways to do this. If they're always automatically taken to the next page after filling out the initial form, then you could package the fields in the onSuccess and pass them to the next form, i.e. something like

MktoForms2.whenReady(function(form){
form.onSuccess(function(vals,tyURL){
var tyLoc = document.createElement("a");
tyLoc.href = tyURL;
tyLoc.hash = encodeURIComponent(JSON.stringify(vals));
document.location = tyLoc;
return false;
});
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If they can get to the next page at a later time, though, use a cookie.

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Pulling Field from One Form to Another

Certainly a couple of ways to do this. If they're always automatically taken to the next page after filling out the initial form, then you could package the fields in the onSuccess and pass them to the next form, i.e. something like

MktoForms2.whenReady(function(form){
form.onSuccess(function(vals,tyURL){
var tyLoc = document.createElement("a");
tyLoc.href = tyURL;
tyLoc.hash = encodeURIComponent(JSON.stringify(vals));
document.location = tyLoc;
return false;
});
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If they can get to the next page at a later time, though, use a cookie.

Samantha_Cossum
Level 3

Re: Pulling Field from One Form to Another

Thanks Sanford Whiteman‌! That seemed to work to get values into the URL (this is my url when I submit the form now https://go.schedulinginstitute.com/RCBC-App-Testing-2.html#%7B%22rCBCCurrentAnnualCollections%22%3A%... ). But how do I pull those values into the next form? If it was in more of a url parameter format I know how I could those into hidden fields but not sure how to pull the values out of this.

Also, is there a way to only pass certain values? I really only need two of them from the first form. 

Really appreciate your help! 

SanfordWhiteman
Level 10 - Community Moderator

Re: Pulling Field from One Form to Another

On the next page, you can run

MktoForms2.whenReady(function(form){
var hashJSON = document.location.hash.substring(1),
mktoFields = JSON.parse(decodeURIComponent(hashJSON));

form.setValues(mktoFields);
});‍‍‍‍‍‍
SanfordWhiteman
Level 10 - Community Moderator

Re: Pulling Field from One Form to Another

Also, is there a way to only pass certain values? I really only need two of them from the first form.

If you want to pass just certain fields, include them in the includeOnly array.

MktoForms2.whenReady(function(form){
var includeOnly = ["FirstName", "LastName"];

form.onSuccess(function(vals,tyURL){
var tyLoc = document.createElement("a");
tyLoc.href = tyURL;
tyLoc.hash = encodeURIComponent(JSON.stringify(vals,includeOnly));
document.location = tyLoc;
return false;
});
});‍‍‍‍‍‍‍‍‍‍‍
Samantha_Cossum
Level 3

Re: Pulling Field from One Form to Another

All worked perfectly! Thank you so much!!!

Samantha_Cossum
Level 3

Re: Pulling Field from One Form to Another

Sanford Whiteman‌,

Sorry, on more question. It's loading our thank page before the information posts and we trying to also display these numbers in tokens on the follow up page. Is there a way to ensure the information posts before the next page loads? 

My form code is:

<div>
<script src="//app-ab14.marketo.com/js/forms2/js/forms2.min.js"></script>
<form id="mktoForm_6325"></form>
<script>// <![CDATA[
var mktoInstance = '//app-ab14.marketo.com',
munchkinId = '090-EZX-133',
formId = 6325;
// reuse existing fields in demo!
var AnnualCollections = 'rCBCCurrentAnnualCollections',
CurrentRooms = 'rCBCCurrentRooms',
AverageMonth = 'rCBCAverageMonthlyCollections',
MonthlyRoomCollection = 'rCBCCollectionsRoomMonth';

MktoForms2.loadForm(mktoInstance, munchkinId, formId,
function (form){

form.onSubmit(function(form){
var AC = +form.getValues()[AnnualCollections],
CR = +form.getValues()[CurrentRooms],
finalValues = {};

// calc the actual final value and post that to Marketo
finalValues[AverageMonth] = Math.round(AC / 12);
finalValues[MonthlyRoomCollection] = Math.round(AC / 12) / CR;
form.setValues(finalValues);

form.submittable(true);
});
});

MktoForms2.whenReady(function(form){
var includeOnly = ["rCBCCurrentAnnualCollections", "rCBCCurrentRooms"];

form.onSuccess(function(vals,tyURL){
var tyLoc = document.createElement("a");
tyLoc.href = "https://go.schedulinginstitute.com/RCBC-App-Testing-2.html";
tyLoc.hash = encodeURIComponent(JSON.stringify(vals,includeOnly));
document.location = tyLoc;
return false;
});
});
// ]]></script>
</div>