Post

1 follower Follow
0
Avatar

Is there a way to prefill form fields in WordPress using JavaScript?

Frank Hagan

I have a weird request, but it is probably a more common need now that I think about it. I want to use an ONTRAPORT Smart Form inside of WordPress and have the fields prefilled with the logged in user's information. How can I do that?

Answered

Official comment

Avatar

Our QA developer Anthony Baldwin developed this code to do so. Note that we cannot train you on how to use Javascript, but your developer will know how this works:

<script type="text/javascript">
var count = $('input[name*="email"]').length;
var current = 0;
var contactfirst = "[pilotpress_field name='First Name']";
var contactlast = "[pilotpress_field name='Last Name']";
var contactemail = "[pilotpress_field name='Email']";
while (current<count) {
document.getElementsByName('firstname')[current].value = contactfirst;
document.getElementsByName('lastname')[current].value = contactlast;
document.getElementsByName('email')[current].value = contactemail;
current++;
}
</script>
Frank Hagan

Please sign in to leave a comment.

1 comment