how do i identify from what website a user submitted a form?

129 Views Asked by At

I want to identify from what website a user has submitted a contact form.

The contact form is an include file (.shtml) and this file is being used on two different websites. Since it's only one file (include), will a javascript if...else statement or switch statement do this? If so, how would it be written?

I'm new to JavaScript and haven't had to write much so would appreciate help with the syntax.

I thought about adding two hidden fields on the one include contact form, with IDs of the two different sites:

html

<input type="hidden" data-id="new_student_signup" name="student_signup" value="" />
<input type="hidden" data-id="existing_student_signup" name="existing_student_signup" value="" />

js

if(data-id="new_student_signup"){

  // this user came from the new student website

   code if this condition is true
 }

else if(data-id="existing_student_signup"){

  // this user came from the existing student website

   code if this condition is true

 }

Or should I just create a new include file for the other website and add the hidden field to that file instead of using javascript on one include?

I'll admit, that seems easiest for a JavaScript novice like me, but I do like the idea of learning more JavaScript.

2

There are 2 best solutions below

5
On

You can do it using server side scripting by checking HTTP_REFERRER.

PHP example

<?php
if(!empty($_SERVER['HTTP_REFERRER']) && $_SERVER['HTTP_REFERRER'] == 'http://google.com/')
{
// proceed accordingly
}

You might want to distinguish referrer based on the domain name or more specific data in the URL rather than the whole HTTP referrer string. In that case, take a look at http://php.net/parse_url.


You can do it using JavaScript as well by accessing document.referrer property.

0
On

You could do this in javascript by checking the document.url property and putting that into a hidden input field. However, I think you'd be better off just checking this in server side programming code. You can just check the current server you're on and use that to determine which form it is. You can do this by getting the SERVER_NAME server variable, this will tell you the domain you're currently on.