Brian
Brian

Reputation: 31

Trying to pre-populate form fields with specific URLS

One of our affiliates wants us to be able to pre-populate fields on our form, based on the URL. Using something such as an "s5 parameter". For example:

Here is the example link:

www.smartreliefrx.com/qualify/new/diabetes/?a=1476&oc=172&c=1205&m=2&s1=#affid#&s2=#s2#&s3=#s1#&#s5#

How would I go about doing that? I am only using JavaScript.

Thanks!

Upvotes: 0

Views: 1306

Answers (1)

user1572796
user1572796

Reputation: 1057

Get rid of the #'s around the parameters in the url or you'll need to change the function below. Also change the var url to the window.location, I think its right but double check, try this:

See it in action: http://jsfiddle.net/wp4Ls24n/2/

function populateForm() {
  var formVal = '';
  var url = 'http://www.smartreliefrx.com/qualify/new/diabetes/?a=1476&oc=172&c=1205&m=2&s1=#affid&s2=#s2&s3=s1#&s5=someValue';
  //var url = window.location.search.substring(1);
  url = url.split('&');
  for(var i=0; i<url.length; i++) {
    var pair = url[i].split('=');
    if(pair[0] == 's5') {
      formVal = pair[1];  
    }
  }
  document.getElementById('someInput').value = formVal;
}

populateForm();


<form>
    <label>Some Input:</label>
    <input id="someInput" />
</form>

Upvotes: 1

Related Questions