简体   繁体   中英

javascript to get previous url of visitor from contact form

I want to track my prospects from which URL(like fb, g+ or mailer) they have visited my website.This is all is for understanding visitor for better digital marketing.

My Fiddle: http://jsfiddle.net/dSp4g/

Please help me with code as below

Thanks in advance

HTML

<div class="form-group">
<label for="exampleInputEmail">Full name</label>
<input type="text" class="form-control " id="exampleInputName" placeholder="Enter your full name" style="color:#000 !important;">
 </div>

<div class="form-group">
<input type="hidden" class="form-control " id="sourcecode">
</div>                                          

<div class="form-group last">
<button class="mailbtn">Submit</button>
</div>

JS

$('.mailbtn').live('click',function(){

            name = $('#exampleInputName').val();

            sc = $('#sourcecode').val();

            $(document).ready(function() { 
                if (sourcecode = $.parsequery().s) 
                    {
                        $("#source").val(sourcecode); }
             });


            $.ajax({
            type: "POST",
            async : false,
            url: "mail.php",
                data: { name:name, sourcecode:sc}
            })

            .done(function( msg ) {
            $('.mail_middle').html('');
            $('.mail_middle').html('We will call you to confirm your delivery address.Thank you.');
            return false;

            });



    });

mail.php

<?php
$to =  array("my_email1","my_email2");
$message .= "<table border='1'>";
$message .= "<tr><td>Name    </td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Source   </td><td>".$_POST['sc']."</td></tr>";
$message .= "</table>";

$headers = "MIME-Version: 1.0\r\n"; 

$headers .= "Content-type: text/html; charset=utf-8\r\n"; 

$headers .=  'from: '.$from .'' . "\r\n" .

            'Reply-To: '.$from.'' . "\r\n" .

            'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
   mail($row,$subject,$message,$headers);
}

echo "Mail Sent.";
die;
?>

Change $_POST['sc']

$message .= "<tr><td>Source   </td><td>".$_POST['sc']."</td></tr>";

to $_POST['sourcecode']

$message .= "<tr><td>Source   </td><td>".$_POST['sourcecode']."</td></tr>";

as sourcecode is the name (where sc is the value)

data: { name:name, sourcecode:sc}

HTML

add a

<input type="hidden" name="the_ref" value="<?= $_SERVER['HTTP_REFERER']; ?>">

JS

modify to

data: { name:name, sourcecode:sc, the_ref:ref}

PHP

add a

$message .= "<tr><td>Visitor came from </td><td>".$_POST['the_ref']."</td></tr>";

your jquery implementation fails because in your php you are trying to read the $_POST['sc'] instead of $_POST['sourcecode']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM