Link to home
Start Free TrialLog in
Avatar of jaysch
jayschFlag for United States of America

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaysch

ASKER

Dave,

Your code did the trick, works perfect!

Thanks so much for your help.
You're welcome, glad to help.