How to pass querystring parameters to a hidden form field using Javascript

Dear Experts,

I have successfully passed querystring parameters from a link on a page:

<a href="HomeQuote.html?code=Travelers&desc=Home">

Open in new window



to  another form page and displayed the parameter value on the page as displayed text.

I'm using this code on my form page to retrieve the querystring values:

 
<script type="text/javascript">
var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}
</script>
<script type="text/javascript">
qsParm['code'] = null;
qsParm['desc'] = null;
qs();
</script>

Open in new window


This is the inline code I'm using to display the querystring params as live text:

 
<h2> <script type="text/javascript">
if (qsParm['code'] && qsParm['desc'])
document.write(qsParm['code'] + ' ' + qsParm['desc']);
</script> Insurance Quotation Request - Receive a $25 Mastercard!</h2> 

Open in new window


I also need to pass the same values to a hidden form fields to be included in the generated email. I've tried several methods to pass the param value to the hidden field but nothing seems to work:

 
<script language="JavaScript">
     document.write('<input type="hidden" name="Company" value="' + getQuerystring('code') + '">'); 
document.write('<input type="hidden" name="Type" value="' + getQuerystring('desc') + '">'); 
</script>

Open in new window


I'm not sure if I should use the 'gsParam' value or try to use the Querystring method. I have set form action to method="get" but that doesn't help.

Any ideas would be greatly appreciated. I need any suggestions to be written in javascript.

Thank you!
jayschAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Here is what I would do.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>qsParms</title>
 
<script type="text/javascript">
var qsParm = new Array();
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
			}
		}
	}
</script>
<script type="text/javascript">
qsParm['code'] = null;
qsParm['desc'] = null;
qs();
</script>

</head>
<body>
<h1>querystring params</h1>
<h2> <script type="text/javascript">
if (qsParm['code'] && qsParm['desc']) {
	document.write(qsParm['code'] + ' ' + qsParm['desc']);
	}
</script> Insurance Quotation Request - Receive a $25 Mastercard!</h2>
<form action="#">
<input type="text" name="Company" id="Company" value="" /><br /><br />
<input type="text" name="Type" id="Type" value="" />
</form> 
<script type="text/javascript">
if (qsParm['code'] && qsParm['desc']) {
	document.getElementById('Company').value = qsParm['code'];
	document.getElementById('Type').value = qsParm['desc'];
	}
</script>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jayschAuthor Commented:
Dave,

Your code did the trick, works perfect!

Thanks so much for your help.
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.