2 forms 1 page URL parameter to fill both

Hey. I’m relatively new to this. Mostly a copy&paste, tweak a little kinda guy. I have a code with a hidden field that is filled by a url parameter which is an affiliate id. Completed code results in button that when clicked takes folks to that affiliates site.
I use a separate js code to fill the code above.
Your mission, if you choose to accept it, is I want a second button going to a totally different site with a different id. On a different part of my site. Both filled by url parameters.
Something like https://blahblah.com/?id=abc&id2=123

Hidden field button code…

<script type="text/javascript">function getURL(val){ if(!val){ alert('Please enter the Code you were given. IF you were not given a Code, please enter Profitshare. Thank You!'); return false; } else{ base = 'https://site.com/?referral='; location = base + val; return false; }}</script><form id="form1" onsubmit="return getURL(this.id.value)" method="post" name="form1" action=""><label><input type="hidden" name="id" value="profitshare"></label><BR><label><input value="Visit" name="Submit" type="image" src="https://site.com/assets/images/image01.png?v16534898140951" alt="Visit Site" style="width:200px; height:48px;font-size:10pt;"> </label></form>

Form filler code…

<script type="text/javascript" language="JavaScript">
<!-- Copyright 2006 Bontrager Connection, LLC
function FillForm() {
// Specify form's name between the quotes on next line.
var FormName = "form1";
var questionlocation = location.href.indexOf('?');
if(questionlocation < 0) { return; }
var q = location.href.substr(questionlocation + 1);
var list = q.split('&');
for(var i = 0; i < list.length; i++) {
 var kv = list[i].split('=');
 if(! eval('document.'+FormName+'.'+kv[0])) { continue; }
 kv[1] = unescape(kv[1]);
 if(kv[1].indexOf('"') > -1) {
 var re = /"/g;
 kv[1] = kv[1].replace(re,'\\"');
 }
 eval('document.'+FormName+'.'+kv[0]+'.value="'+kv[1]+'"');
 }
}
FillForm();
//-->
</script>

Hi @justgoodkarma, welcome to the forums.

[off-topic]
When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.
[/off-topic]

Thank you. My old brain was searching how…

1 Like

Hello justgoodkarma

I read through your questions.
I’m finding it a bit difficult to follow.
Can you explain a little more what you are trying to achieve?
Also, can you explain a little more about your server setup?
Are you using a server side language? (for example php).
The reason I ask is, it might be really easy to accomplish what you are trying to achieve on the server side.

2 different opportunities on one page with a button to access each. Member puts his id at end of parameter to fill hidden field in button 1. Works slick, perfect with 1 button. I want to more or less duplicate this function with different opportunity & different button.
Button ‘filled’ by hidden field in form.
2 forms filled by one url parameter…site/?id1=abc&?id2=123
Tried form1 and form2. Always just initiates first form…

This is a sitebuilder type site like Wix or Weebly. Add code no access to server.

As I said before, I’m finding it very difficult to follow what you are trying to do, and what your code is trying to do.
As gandalf456 said, format your code…

The only thing that I can see at the moment is, your function FillForm, has the variable FormName and it’s hard coded to “form1”.
I assume that this function sets the values of which you speak in the forms, but as it’s hard coded, it will always set the value in form1.
When you call FillForm, you need to send it the name of the form, in which you want to fill in the values.
Then you need to call it two times, once for form1 and once for form2.

Thank you for putting up with me. How exactly do I call it two times?

FillForm('form1');
FillForm('form2');

I will try. Going to unplug for awhile. Thank you!

Pleasure.
Good luck.

Your solution may work but may be incomplete. That is for the form filling script. Remember, 2 forms with submit button for each. What modifications would I need to make on second button script? Tried naming it form2, changing id to id2, etc…

In the second form, the ID of the form tag needs to be “form2”.
Your function definition for FormFill, needs to change to

function FillForm(FormName){
...

And remove the line var FormName = “form1”; as that variable is now being passed into the function.