Passing URL String Variables to Prefill an Ontraport Form

If you are using the “Javascript Snippet” method of getting a Ontraport Smart form onto your WordPress page and want to automatically fill in a field on the form (e.g., firstname) using a URL string variable (e.g., ?fn=John), I got it to work using the following Javascript:

<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
window.onload = setVar;
function setVar() {
var theform = document.getElementsByTagName("form");
theform[0].elements["firstname"].value=getParameterByName("fn");
}
</script>

Note: this assumes there’s one form on the page. If there are multiple forms on the page, the first one is referenced by theform[0], the second one by theform[1], etc.

Note 2: works with both visible and hidden form fields (I’m actually using it with a hidden field).

Leave a Reply

Your email address will not be published. Required fields are marked *