View Full Version : parse parts of a URL and insert into form fields

07-29-2009, 01:26 AM
Hello all,

I am sending out a small email blast to my clients. I would like to offer them a free download of a report, but I don't want to have them re-fill all of their information. Therefore, I would like to have someone help me out with a bit of code that will parse the URL and place the separated items into the form fields. Then they can hit submit and be done.

I have the following URL:


I would like to have a form with the following fields:

Once a person has landed on the page, the forms will automatically fill out and they will hit submit.

This is what I have so far...but it is not parsing correctly. Can anybody help me out to add in the right code?

<SCRIPT type="text/javascript">
function getQuerystrings(inputId) {

var urlString = parent.document.URL;
var queryString = urlString.substring(urlString.indexOf('?'),urlString.length);

inputObj = document.getElementById(inputId);
inputObj.value = queryString;
<BODY onload="getQuerystrings('urlQuery')">
<form id="form1">
<input id="urlQuery" type="text" value="">
<input id="urlQuery" type="text" value="">
<input type="button" value="Submit">


Old Pedant
07-29-2009, 04:45 AM
function processQueryString( )
var qs = location.search.substring(1); // search is all of URL from ? onwards...so zap the ?
var pairs = qs.split("&")
for ( var p = 0; p < pairs.length; ++p )
var temp = pairs[p].split("=");
var name = temp[0];
var value = unescape( temp[1] ); // unencode, actually
var formfield = document.form1.elements[name];
if ( formfield != null )
// if a field by that name actually exists...
formfield.value = value; // then shove in the value

Something like that, I would think.

Note that it is POINTLESS to give an ID to a form field. You MUST give it a name if you want its value to be seen on the page that the <form> posts to (either GET or POST). And so since you must name it, why bother with the ID? And while you *can* give an ID to a <form>, it's more sensible to give it a name, so that you don't need to use document.getElementById.

Also, you can *NOT* give the same ID to more than one object. [Well, you can, but it then needs special handling and getElementById will NOT work.]

Finally, an <input type=button> will *NOT* submit a <form> unless you use javascript for the onclick event to do it. You need to use <input type=submit>.

In short, start over.

So your <form> should have looked like:

<form name="form1">
<input name="id" type="text" value="">
<input name="first" type="text" value="">
<input name="last" type="text" value="">
<input name="city" type="text" value="">
<input type="submit" value="Submit">

Old Pedant
07-29-2009, 04:46 AM
By the by, this would be better done with a server-side script. PHP or ASP or JSP or ASP.NET or whatever. Easier to do better error checking. Not necessary, but a good idea.

07-29-2009, 06:50 AM
I know that it would be better off using PHP, but I do not have that option.

I do however need this to work within a straight HTML environment...nothing fancy, I am just trying to capture the people's info in the link and save them the hassle of filling out the form. I am sending an email out based on the Salesforce database that I have.

As for using "id" that was an overlook on my part. The fields that I will have are; first, last, email, company, telephone.

Anyways, I appreciate your help, but the code that you provided does not populate the fields upon entry...any help would be greatly appreciated.

07-29-2009, 06:53 AM
I had to include an "onload" event.

This is great. Thanks for your help.