Last active
September 23, 2021 23:47
-
-
Save derekcavaliero/f79007a21233d8358636faa5199da165 to your computer and use it in GitHub Desktop.
HubSpot Form - Dynamic Redirect URL from Submitted Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hbspt.forms.create({ | |
portalId: "XXXXXX", | |
formId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", | |
inlineMessage: 'Please wait...', | |
onFormSubmit: function($form){ | |
// We'll store our "object" to transform into a query string here. | |
// If you want to hard-code a value that isn't part of the form - add it in here. | |
var queryObj = { | |
q3: 'myself' | |
}; | |
// Mapping fields to mulitple query strings for redirect location. | |
// Use a single string value to make a 1:1 mapping, use an array to map one field to multiple query strings. | |
var queryMap = { | |
email : ['q1', 'cf3'], | |
event_survey: 'q2', | |
firstname : ['q4', 'cf1'] | |
}; | |
for ( var prop in queryMap ) { | |
if ( !queryMap.hasOwnProperty( prop ) ) | |
continue; | |
var fieldVal = $form.find( 'input[name="' + prop + '"], select[name="' + prop + '"]' ).val(); | |
if ( $.isArray( queryMap[prop] ) ) { | |
$.each( queryMap[prop], function( index, value ) { | |
queryObj[value] = fieldVal; | |
}); | |
} else { | |
queryObj[queryMap[prop]] = fieldVal; | |
} | |
} | |
setTimeout( function() { | |
window.location = "https://some-website.com/foo/bar?" + $.param( queryObj ); | |
}, 1000 ); // Redirects to url with query string data from form fields after 1000 milliseconds. | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment