0

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!

Brian
  • 31
  • 4
  • What code do you have so far? What is your specific problem? – MJC Oct 10 '14 at 17:31
  • Is this not the same question as what you asked before? http://stackoverflow.com/questions/26188813/trying-to-grab-url-parameters-and-post-them-with-form – benomatis Oct 10 '14 at 17:36
  • it's not hard to find how to parse query string from url into javascript object or array by doing a web search. You need to try something and provide an attempt at least before asking questions here – charlietfl Oct 10 '14 at 17:46
  • @charlietfl You are making assumsions that i have not tried to work on this, before posting here? I didn't realize there were such extreme rules in place that I can't ask a question, unless I ask Google first. JavaScript is not my strongest language, so if I need help with it, I don't see anything wrong with asking people who do. And you don't need to answer it either. – Brian Oct 10 '14 at 18:12
  • Some links found in a 3 minute search http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url , http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript . You don't reference any research effort in question, therefore it is generally assumed very little effort was attempted. If you have researched, provide some feedback on those efforts and findings – charlietfl Oct 10 '14 at 18:39
  • Charlietfl, Those are both great links you have provided. However, not knowing who I am, really makes it irrelevant to you. I understand the point you are making, but for someone who dyslexia and bunch of other process disorders, talking to actual people help me. Glad those solutions worked for them, however they are not me. Thank you – Brian Oct 10 '14 at 19:45

1 Answers1

1

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>
user1572796
  • 1,057
  • 2
  • 21
  • 46
  • Hi user1572796, unfortunately I can't remove the #'s. They are strict with their links. What should I do to change this function to get it to work? Thanks! – Brian Oct 13 '14 at 13:05
  • Try something like this then if the #'s are always there: http://jsfiddle.net/wp4Ls24n/3/ – user1572796 Oct 13 '14 at 17:13