0

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.

woninana
  • 3,409
  • 9
  • 42
  • 66
  • Please clarify what you mean by referral, and what you're trying to do. And how is email involved? Also, I noticed your js code expects a div where you actually have and input (`#WelcomePlaceHolder`), followed by an unmatched ``. – bfavaretto Jan 17 '13 at 01:32
  • The referral link where the user came from. I want to send that link to my email. – woninana Jan 17 '13 at 01:50
  • Do you want the URL, or the corresponding message from the `msgs` array? What if the URL isn't found in the array? – Barmar Jan 17 '13 at 01:54

1 Answers1

0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I just want the URL. How can I show display the url only to the email? – woninana Jan 17 '13 at 02:16
  • Is this correct for displaying into the email: ' $message .= 'Referral Site : ' . $_REQUEST['referrer'] . ''; ' – woninana Jan 17 '13 at 02:22
  • Yes, it's just like any other form field. – Barmar Jan 17 '13 at 02:24
  • It displays the message not the URL. – woninana Jan 17 '13 at 02:26
  • You may need to fix the problem bfavaretto mentioned in his comment. Your HTML isn't valid and it may be causing problems. This means you'll need to use different variables for where to add the CloseButton and where to add the referer value. – Barmar Jan 17 '13 at 02:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22869/discussion-between-winona-and-barmar) – woninana Jan 17 '13 at 02:35