Trying to pass URL parameters to input fields (NOT

2019-09-18 01:31发布

问题:

I've used some time researching this, both here on SO and through Google. However I have not been able to find anything that helps me. Here is the scenario:

I have a page (MP), where I send people on to an affiliate (AP) of mine. It's very important that they fill out the fields on the AP correctly, else I will not get credited. Therefore I would like my affiliate link (AL) to include parameters to pass on to the form on the AP. The problem, however, is that the "form" on the AP is not actually a form, but a combination of input fields. Here is a snippet from the AP (remember I can not edit this code):

<tbody class="original">
<tr><td colspan="3"><p><strong>* required</strong></p></td></tr>
<tr>
<th width="180"><label for="affiliateID">Affiliate ID</label></th>
<td width="250"><div class="text"><input id="affiliateID" name="affID" class="text"></div></td>
</tr>
<tr>
<th width="180"><label for="txtEmail">Your e-mail address</label></th>
<td width="250"><div class="text"><input id="txtEmail" name="email" class="text"></div></td>
</tr>
<tr>
<td colspan="3">
<input id="submitInfo" class="submit" type="submit" value="Sign-up">
</td>
</tr>
</tbody>

My question is, will I be able to address and "pre-fill" the fields #affiliateID and #txtEmail by passing the values in my url from MP? I've tried to use: http://www.APURL.com?affiliateID=AF54728 and other stuff as well, but I am simply not able to get it to "prefill". Is this only possible to do if the AP is using a traditional HTML form or will I somehow be able to include it in the linking URL, so the AP recognizes that I want to pass a value to a certain field on his page? I was thinking there might be a way to address the ID of the input in the URL.

Hope someone can help me out here.

Thanks in advance.

回答1:

This is possible even if your input fields are not part of a form element. Here is a working example to set the value of your affiliateID field.

function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return unescape(qs[1]);
}
var inp = document.getElementById("affiliateID");
inp.value = getQuerystring("affiliateID", "no ID given");

Update:

I misunderstood the question. This is not your website you're talking about, and you wanted to know if it was possible to change the form values using URL parameters. This is not possible. It is, however, common, that such a page is designed in a way that it accepts parameters via query string. You should ask the site's owner and ask him if you can pass the affiliate id somehow via URL.