0

I'm trying to set the value of a hidden field in my form through a URL parameter.

Here is the form:

<form accept-charset="UTF-8" action="/thanks" class="infusion-form" method="POST">
    <input name="inf_field_LeadSourceId" type="hidden" value="null" />
        <input class="infusion-field-input" id="inf_field_FirstName" name="inf_field_FirstName" placeholder="First Name *" type="text" required/>
        <input class="infusion-field-input" id="inf_field_Email" name="inf_field_Email" placeholder="Email *" type="text" required/>
    <div class="infusion-submit">
        <button class="infusion-recaptcha" type="submit">Submit</button>
    </div>
</form>

I need to set the value of this field, specifically:

<input name="inf_field_LeadSourceId" type="hidden" value="null" />

with a url parameter.

Ideally, I would like it to be something like this:

https://website.com/page?leadsource=123

So it would set that field to have the value of "123"

I tried doing this using the javascript code below, but no luck :(

<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results === null ? 120 : decodeURIComponent(results[1].replace(/\+/g, " "));

}

var LeadId = getParameterByName("leadsource");
jQuery(".infusion-form input[name='inf_field_LeadSourceId']").val(LeadId);
</script>

Any advice on how I can tweak my javascript to prefill my form with the URL parameter?

Matt
  • 23
  • 1
  • 6

2 Answers2

0

JavaScript isn't the best with URL parameters. If I'm understanding your question correctly, it'd probably be ideal for you to simply inject the parameter directly into the HTML field with PHP (requiring no JavaScript).

<input name="inf_field_LeadSourceId" type="hidden" value="<?php echo $_GET['leadsource']; ?>" />
Joel Rummel
  • 802
  • 5
  • 18
  • Thats a good idea, but will '$_GET['leadsource']' automatically pull the value in from my URL parameter? One thing is that I can't very easily inject a bunch of PHP code into my landing page software, but I can add a bunch of custom JS - thanks for your time! – Matt Aug 06 '18 at 19:01
  • If your URL is https://website.com/page?leadsource=123 , then the above will result in your input's value being set to 123 right before the page is served to the user. Another option would be to use the same PHP snippet in your JavaScript code, which would replace your getParameterByName() function (but that'd only work if your script is being inlined). – Joel Rummel Aug 06 '18 at 19:07
  • Unfortunately the software I'm using isn't allowing me to execute PHP code in their builder :( - any suggestions on how to populate that field with JS? – Matt Aug 06 '18 at 19:47
  • This should help out: https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters – Joel Rummel Aug 06 '18 at 20:06
0

Here's the javascript I used to base my solutions filling Infusionsoft hidden fields values.

Also may also try to debug your javascript flow to see if you're catching and parsing the value properly (for example, adding conlose.log(var) line).

Y. E.
  • 687
  • 1
  • 10
  • 29