Hello everyone. I will show you how to insert dynamic content from a CMS collection anywhere in a form using Webflow’s Embed element. In this example, I show you how to create a post method with a dynamic action URL (pulled from CMS). This specific example was used to send form data to Pardot, but you can use the code in your own way to insert dynamic CMS data into your for
-
Drag and drop a Form Field element into your site.
-
Assign class, id, and names to the Form, the Field Label, Text Field, and the Submit Button.
-
Style these four elements however you want your final form to look.
-
Delete the form (Yes delete it, don’t worry - you are about to embed a new custom one with the same style).
-
Now drag and drop an Embed element where you want your form to appear on your site.
-
Paste the following code into the embed element, replacing the bold items with the Class names you assigned in step 2:
<script type="text/javascript" language="JavaScript"><!-- //Creates the form wrapper that will hold the form contents document.write('<form '); document.write(' class="YOUR-FORM-CLASS-NAME-HERE" '); document.write(' name="YOUR-NAME-HERE" '); document.write(' data-name="YOUR-NAME-HERE" '); document.write(' id="YOUR-ID-HERE" '); document.write(' method="post" '); document.write(' action="URL_FROM_CMS_HERE">'); //Creates a label document.write('<div '); document.write(' class="YOUR-LABEL-CLASS-NAME-HERE">'); document.write('<label> '); document.write(' Label'); document.write('</label> '); document.write('</div> '); //Creates a form input field document.write('<input '); document.write(' type="text" '); document.write(' class="YOUR-INPUT-CLASS-NAME-HERE" '); document.write(' name="YOUR-NAME-HERE" '); document.write(' id="YOUR-ID-HERE" '); document.write(' required>'); //remove this if the field doesn't need to be required, and move the > to the line above in the same spot //Creates the button to submit document.write('<input '); document.write(' type="button" '); document.write(' class="YOUR-BUTTON-CLASS-NAME-HERE" '); document.write(' onclick="submitButton()" '); document.write(' value="Submit">'); //Closes the form document.write('</form> '); //--></script>
-
Go to the custom code of the current page and insert this code into the Head section:
<script> function submitButton() { document.getElementById("YOUR-FORM-CLASS-NAME-HERE").submit(); } </script>
-
Publish your site and test your form.
You can now use this custom code to insert CMS elements into any part of a form, including anything from a simple label name to the button method.
Best,
Christopher