Form : preselect a value within a picklist based on referring URL | Community
Skip to main content
Mathias_Saint-1
New Participant
July 19, 2016
Solved

Form : preselect a value within a picklist based on referring URL

  • July 19, 2016
  • 2 replies
  • 5789 views

Hi community,

I am wondering if it is possible to preselect a specific value within a form picklist based on the URL which led to the form.

For example, if I have a form asking the contact to choose the type of product he is interested in : "HR", "INDUSTRY", "CPA" etc.

Can I  automatically display the value "INDUSTRY" if the previous visited page is or contains www.mywebsite.com/industry ?

Your inputs are welcome. Thanks for your help.

Mathias.

Best answer by SanfordWhiteman

MktoForms2.whenReady(function(form){

     var referrerTypeTable = {

          'INDUSTRY' : [/www\.mywebsite\.com\/industry/],

          'HR' : [/www\.mywebsite\.com\/hr/,/www\.mywebsite\.com\/humanresources/],

     };

     Object.keys(referrerTypeTable).forEach(function(type){

          referrerTypeTable[type].forEach(function(pattern){

               if (pattern.test(document.referrer)) {

                    form.setValues({'ProductType':type});

               }

          });

     });

    

});

2 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
July 19, 2016

MktoForms2.whenReady(function(form){

     var referrerTypeTable = {

          'INDUSTRY' : [/www\.mywebsite\.com\/industry/],

          'HR' : [/www\.mywebsite\.com\/hr/,/www\.mywebsite\.com\/humanresources/],

     };

     Object.keys(referrerTypeTable).forEach(function(type){

          referrerTypeTable[type].forEach(function(pattern){

               if (pattern.test(document.referrer)) {

                    form.setValues({'ProductType':type});

               }

          });

     });

    

});

Mathias_Saint-1
New Participant
July 20, 2016

Hi Sanford,

Thanks a lot for the example.

I try to apply this JS to my form. When I try, I know that the targeted field is impacted, but the requested value is not displayed, the field just goes blank (instead of showing by default the "Please choose your product").

Did I miss something ? Below the coding I used. Thanks again for your input.

  1. MktoForms2.loadForm("//app-lon04.marketo.com", "818-MJH-876",2630);
  2. MktoForms2.whenReady(function(form){
  3.     var referrerTypeTable = {
  4.           ' Manufacturing Industry ' : [/go\.cegid\.com\/industry/],
  5.           ' HR ' : [/go\.cegid\.com\/hr/],
  6.     };
  7.     Object.keys(referrerTypeTable).forEach(function(type){
  8.           referrerTypeTable[type].forEach(function(pattern){
  9.               if (pattern.test(document.referrer)) {
  10.                     form.setValues({ 'expertise_c' :type});
  11.               }
  12.           });
  13.     });
  14. });
SanfordWhiteman
New Participant
July 20, 2016

You inserted extra spaces that aren't in my code nor in your form.

     ' HR '

isn't the same as

     'HR'

July 19, 2016

You can prefill for one value but to make a choice you will need to use Javascript on the page to get it done