Passing the Referrer in a Form

One piece of information that you might like to pass along with the information that your visitors enter into a form on your site (particularly if you have lots of pages that link to the form page) is which page that they were on before they came to the form. One example of where this would be particularly useful if the form were there to allow people to report errors found in the content of your site.

It is actually quite easy to add a hidden field to our form to hold this information and then use Javascript to set the value of the field appropriately.

Let's start by adding a field to our form. All we need to do is to define a new hidden field and give it an id (I'll use 'referrer').

<input type="hidden" name="prevpage" value="" id="referrer"/>

Now all that we need to do is to add a small piece of JavaScript to the bottom of your page (added either inside its own script tag or with the rest of your scripts.

document.getElementById('referrer').value = document.referrer;

Now provided that your visitor has JavaScript enabled in their browser and has not disabled the document.referrer header in their browser or firewall the referring page will be passed along with the other fields when the form is submitted. If they do have either Javascript or that header disabled (or they go directly to your page by typing the address into their browser bar, or they are using IE and were last on a secure web page) then the hidden field will still contain blank when it is passed.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow