1

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.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
raydev
  • 25
  • 1
  • 6
  • 1
    Why do you need javascript to know rather than the server? – wheresrhys Feb 01 '12 at 11:57
  • @wheresrhys -- I didn't see your post till now. I really want to know what to do in the html. If it requires a server scripting language to get the information, then I can hand that off to a programmer. I just need to know the best way to tag the information in the form fields (hidden from the user) so that that data will be submitted with the form. – raydev Feb 01 '12 at 14:02
  • @raydev: I think what wheresrhys is asking is which code needs to know what website the form was submitted on? Is it something that needs to be stored along with the other form data on the server? Or is it something you need to know in order to present the HTML of the form differently? – Paul D. Waite Feb 01 '12 at 15:12

2 Answers2

1

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.

Gajus
  • 69,002
  • 70
  • 275
  • 438
  • Thanks, Guy. I am really a novice at javascript. Care to help a student out by adding the code? – raydev Feb 01 '12 at 11:52
  • I provide references and keywords to learn stuff, but not final examples. This is generally the idea behind stackoverflow. – Gajus Feb 01 '12 at 11:56
  • Thanks. I googled the document.referrer property [link](http://www.netmechanic.com/news/vol4/javascript_no14.htm)here. The referring page is an include file, so i'm not sure how to specify the "referrer" site the user used to fill out the contact form. i don't write any server-side language, so would adding just the two hiddenfields to the include file and letting a programmer know the hiddenfield IDs to write the correct script? – raydev Feb 01 '12 at 12:09
  • thanks, Guy for your help. StackOverflow posters provide alot of help, even code samples. – raydev Feb 01 '12 at 12:20
  • Just keep in mind that the referer header isn't safe to use and can easily be spoofed. – daniel Feb 01 '12 at 12:55
0

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.

Shawn Steward
  • 6,773
  • 3
  • 24
  • 45