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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.