SClick
September 28, 2022, 2:39pm
1
Hi!
We’re looking for a way for our client to add their preferred target URL into a custom field on a post, which then defines the redirect of the form (using Hubspot embed as the client prefers this to using something like Gravity etc).
Any help appreciated!
Hi, @SClick Thanks for the question. Hey, @Anton @louischausse , have you tacked anything similar? Thank you for taking a look! — Jaycee
SClick
September 29, 2022, 8:43pm
3
We’ve actually got this working!
<script charset=“utf-8” type=“text/javascript” src=“//js.hsforms.net/forms/v2-legacy.js ”></script>
<![endif]-->
<script charset=“utf-8” type=“text/javascript” src=“//js.hsforms.net/forms/v2.js ”></script>
<script>
hbspt.forms.create({
portalId: “xxxxxxx”,
formId: “xxxxxxxx-xxxx-xxxxxxxx-xxxxxxxxx”,
inlineMessage: ‘Success’,
onFormSubmit: function($form){
setTimeout( function() {
var formData = $form.serialize();
window.location = “[acf field=“target_url”]”
}, 250 ); // Redirects to url from advancved custom fields after 250 milliseconds.
}
});
</script>
Anton
September 29, 2022, 8:49pm
4
Awesome!
just one recommendation: Change “onFormSubmit” to “onFormSubmitted” since this will trigger the redirect after the form is submitted. When you’re working with “onFormSubmit” submissions can get lost/not be submitted and the user/visitor is just redirected
best,
Anton
SClick
September 29, 2022, 9:00pm
5
Ah yes amazing thank you for your help!
Anton
September 29, 2022, 8:38pm
6
Hi @SClick ,
you can modify the embed code with the “onFormSubmitted” option from the advanced form options .
default embed code:
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "xxxxxxx",
formId: "xxxxxxxxxx",
version: "V2_PRERELEASE"
});
</script>
modified embed code:
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "xxxxxxx",
formId: "xxxxxxxxxx",
version: "V2_PRERELEASE",
onFormSubmitted: "https://yourredirect.com"
});
</script>
hope this helps
best,
Anton