Forum Moderators: open

Message Too Old, No Replies

Pass Page URL through hidden form field?

How do you pass the URL through a forms hidden field

         

mrproducer

5:03 am on Mar 6, 2008 (gmt 0)

10+ Year Member



I have a client who uses formmail.pl for his contact forms.
He wishes to see which clients send him leads who use his form.
I thought to add a "?ref=12345" at the end of the URL, and pass the URL through a hidden field in the form.

for example. [mywebsite.com?ref=somecode...]

Does anybody know the JavaScript that I need to add to the contact form to do this and pass along the URL through the form?

I did it before, and have forgot.. :)

Thanks so much...

httpwebwitch

2:22 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The best way to do this doesn't involve Javascript.

I'd suggest a server-side script that puts the ref code in a Session. The first time a page sees ?ref=123, put the value 123 in a session, then redirect the page to itself, with the "ref" removed. for example:

http://www.example.com/page.htm?ref=1234
redirects to
http://www.example.com/page.htm

Then on your contact form, you can look in the session and inject the ref code into a hidden variable.

chriswo

5:26 am on Mar 9, 2008 (gmt 0)

10+ Year Member



HTTPWebWitch's idea will work fine. (There are two ways the word "session" is used: one with server-side, in-memory variables and the other is with session-level cookies - I think HTTPWebWitch was recommending the first, which is a far easier approach than doing it with cookies).

However, if you can't add code to the FormMail script, then you will have to get the referral code into a hidden field before the customer submits it.

The trick is: how to get the referral code recorded during the first page view on your site? (If the customer arrives at your homepage with ?ref=1234, then that value will be lost by the time they get to your contact page).

I would recommend:

1) Consinder using the URL of the page linking to yours instead of a referral code (this might be easier than tracking referral codes for other sites to use). This value is sent automatically whenver someone clicks on a link. However, this won't work if the other site is using a redirector to count their outbound clicks (in that case you will have to use referral codes like ?ref=12345).

2) On every page, adding a small script that determines who sent you this customer, and recording that value in the customer's a session-level cookie. Advantage #1: they don't have to link directly to your Contact form. Advantage #2: they can look around first before they fill out your Contact form.

3) On the Contact form, populating a hidden field that FormMail will collect.

To do this, the first function you will need is for reading cookie values:


function getCookieValue (name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

The second function will set the cookie whenever it is blank (i.e. during the first page view to your site). This script sets a session-level cookie, and it will be deleted/cleared as soon as the customer closes his/her browser:


var REFERRER_COOKIE_NAME = "referrer";
var REFERRER_VALUE_IF_BLANK = "REFERRER_WAS_BLANK";
function setReferrerIfBlank()
{
var currentReferrer = getCookieValue (REFERRER_COOKIE_NAME);
if (currentReferrer == null ¦¦ currentReferrer == "")
{
// you can replace document.referrer with the referral code you want to capture
if (document.referrer == "")
{
document.cookie = REFERRER_COOKIE_NAME + "=" + REFERRER_VALUE_IF_BLANK + "; path=/";
}
else
{
// you can replace document.referrer with the referral code you want to capture
document.cookie = REFERRER_COOKIE_NAME + "=" + escape(document.referrer) + "; path=/";
}
}
}

Finally, on your Contact Us page, you will add the hidden field with the value from the cookie:


document.write("<input type='hidden' name='referrer' value='" + getCookieValue(REFERRER_COOKIE_NAME) + "'>");

Hope that helps.

mrproducer

3:30 am on Mar 12, 2008 (gmt 0)

10+ Year Member



thanks so much!