Question

I want to display the referral link to my email. This is what I got so far.

<form method="POST" action="javascript:" name="form">
            <br /><label> <input type="text" name="text" id="text" placeholder="Name" value="" /> </label>
            <br /><label> <input type="text" name="email" id="email" placeholder="Email" value="" /> </label>
            <br /><label> <input type="text" name="phone" id="phone" placeholder="Phone" value="" /> </label>
            <br /><label> <input type="text" name="company" id="company" placeholder="Company" value="" /> </label> 
            <br /><label> <textarea name="textarea" id="textarea" placeholder="Message" rows="3" value="0" /></textarea> </label>
            <br /><label> <input type="text" name="referrer" id="WelcomePlaceHolder" placeholder="Referrer" value="" style="display: none;"/></textarea> </label>
        <br />
            <br /><label> <input class="homebotton" type="button" name="button" id="button" placeholder="button" value="Send" /> </label>
         </form>

Form sent to email:

$message .= '<tr><td>Name : </td><td>' . $_REQUEST['text'] . '</td><tr>';
    $message .= '<tr><td>Email : </td><td>' . $_REQUEST['email'] . '</td></tr>';
    $message .= '<tr><td>Phone : </td><td>' . $_REQUEST['phone'] . '</td></tr>';
    $message .= '<tr><td>Company : </td><td>' . $_REQUEST['company'] . '</td></tr>';
    $message .= '<tr><td>Message : </td><td>' . $_REQUEST['textarea'] . '</td></tr>';
    $message .= '<tr><td>Referral Site : </td><td>' . $_REQUEST['referrer'] . '</td></tr>'; 

Javascript to retrieve referrer link.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//Add urls regular expressions and your message(any HTML Code) to the array
var msgs = [
//null url : to show a message for direct traffic (no referer, some one who remember your site url!)
{'url':null,                           'msg':'I am glad you remember my site URL, enjoy your stay'}
//My url! : show message for referrals from your own site or null so you do not annoy them
,{'url':/^http:\/\/(\w+\.)?moretechtips\.net/,    'msg':null}
//Other urls
,{'url':/^http:\/\/(\w+\.)?google\.com/,      'msg':'Welcome googler, Hope you will find what you looking for'}
,{'url':/^http:\/\/(\w+\.)?dzone\.com/,         'msg':'Welcome fellow dzone reader, if you like it please vote it up'}
,{'url':/^http:\/\/(\w+\.)?digg\.com/,         'msg':'Welcome digger, if you like it please digg it'}
,{'url':/^http:\/\/(\w+\.)?propeller\.com/,      'msg':'Welcome propeller user, hope you will like it here'}
//generic pattern: to show generic message for referrers that you did not specify one for
//You must keep it at the end of the list as it will match any non empty referrer
,{'url':/^http:\/\//,               'msg':'Hello their.. Hope you will find what you looking for'}
];
function DetectReferrer(){
   var div = $('#WelcomePlaceHolder');
   //if Div was not placed means , not to show message
   if (!div.length) return;
   var ref = document.referrer.toLowerCase();
   var msg = findMatch(ref);
   // if not null msg found
   if(msg) {
      //Add Close Button and Show up message
      div.html( '<a href="javascript:void(0)" class="CloseButton">X</a>' + msg).show('slow',function(){
         //Hide On click close Button
         $('.CloseButton',div).click(function(){ div.hide() })
      });
   }
}
function findMatch(ref) {
   for(var i=0; i<msgs.length; i++)
      if( ( ref=='' && msgs[i].url==null) || (ref>'' && ref.match(msgs[i].url) ) )
         return msgs[i].msg;
   return null;
}

// Call the Referrer Detector function on document ready
$(DetectReferrer);
</script>

This script works. I tried to display the referral link sent to my email but it is empty. The referral link is empty. I hope I'm making sense.

Was it helpful?

Solution

This will send the message from the msgs table in the email.

if(msg) {
   //Add Close Button and Show up message
   div.html( '<a href="javascript:void(0)" class="CloseButton">X</a>' + msg).show('slow',function(){
      //Hide On click close Button
      $('.CloseButton',div).click(function(){ div.hide() })
   });
   // Include message in form data
   div.val(msg);
}

If you just want the referer URL, whether or not it was in the table, just do:

div.val(ref);

before if (msg).

To get this sent to the PHP script, you need to change the form field to:

<input type="hidden" name="referrer" id="WelcomePlaceHolder" placeholder="Referrer" value=""/>

Form fields that have display: none are not sent to the server, that's what type="hidden" is for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top