javascript从联系表单获取访问者的上一个网址

时间:2014-04-03 14:50:08

标签: javascript php jquery html

我想跟踪他们访问我网站的网址(如fb,g +或邮件程序)的前景。这是为了了解访问者以获得更好的数字营销。

我的小提琴:http://jsfiddle.net/dSp4g/

请帮我处理以下代码

提前致谢

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;
?>

2 个答案:

答案 0 :(得分:1)

更改$_POST['sc']

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

$_POST['sourcecode']

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

因为sourcecode是名称(其中sc是值)

data: { name:name, sourcecode:sc}

答案 1 :(得分:0)

HTML

添加

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

JS

修改为

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

PHP

添加

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

你的jquery实现失败了,因为在你的php中你试图阅读$_POST['sc']而不是$_POST['sourcecode']