Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单击事件上将PHP嵌入Javascript_Php_Ajax - Fatal编程技术网

在单击事件上将PHP嵌入Javascript

在单击事件上将PHP嵌入Javascript,php,ajax,Php,Ajax,这是我需要做的。当你点击一个链接时,我希望在PHP中运行一个脚本,记录用户点击链接的内容,并将信息保存到SQL表或文件中 有什么想法吗?您可以开始添加onclick元素。我建议为此使用jquery,但在普通php中,您可以使用: <elem onlcick="log(this)"> 总体思路: HTML <input type="hidden" value="<?php echo $this->username; ?>" id="username">&

这是我需要做的。当你点击一个链接时,我希望在PHP中运行一个脚本,记录用户点击链接的内容,并将信息保存到SQL表或文件中


有什么想法吗?

您可以开始添加onclick元素。我建议为此使用jquery,但在普通php中,您可以使用:

<elem onlcick="log(this)">
总体思路:

HTML

<input type="hidden" value="<?php echo $this->username; ?>" id="username"></input>
<a href="google.com" class="track" data-value="google">Google</a>
PHP

public function track() {
  $query = "insert into trackingTable ('track','username') values ({$_POST['track']},{$_POST['username']});
  $mysql->query($query);
}
简单的

只需在链接上附加一篇ajax文章

  $('a.pdf').click(function(e){
    e.preventDefault();
    var id = $('#userid').val();  
    var href = $(this).attr('href');
    $.post("countingpage.php",{ user: id, page: href}).done(function() {
    //on success of posting to database, it directs to download       
    window.location.assign(href);
     })
   });
然后在您的链接中…如果您在会话中通过php访问用户id…,那么只需将该id插入一个隐藏字段,以便稍后提取

   <input type="hidden" id="userid" value="<?php echo $_SESSION['username']; ?>"></input>

您需要一个HTML页面作为web页面,以及一个php脚本来处理数据库检查。首先是HTML页面。为了简单起见,我将javascript直接放在脚本标记中,但您可能不想这样做。因此,首先,只有主标记和空脚本标记:

<html>
    <head>
        <title>AJAX and MySQL Demonstration</title>
        <script type="text/javascript"><script>      <!--Your script goes here-->
    </head>
    <body>
        <a href="www.example.com" id="link">Link</a> <!--This is the link-->
    </body>
</html>
这有点复杂。每次单击链接时都会调用第一行中的函数。第二行包含用户名。如果你有一个用户名变量,把它放在那里。如果你在PHP中有它,或者你想使用IP地址,只需要把那一行去掉。第三行使用jQuery针对AJAX的post方法。它使用post方法将变量
user
发送到第一个参数中引用的PHP脚本。第二个参数指定使用http“post”方法发送
user
变量,并创建一个可从PHP脚本
PHPuser
访问的post变量。您可以使用任何您想要的名称,但我将其命名为
PHPuser
,以区分它和javascript
user
变量。如果你愿意,你也可以叫它
user
。第三个参数是一个函数,AJAX完成后将调用该函数。变量
result
,您可以随意命名,它保存PHP脚本返回的数据。如果一切正常,编写的PHP脚本将(根据设计,而不是自动)返回“1”。现在,对于该脚本:

<?php
    if (isset($_POST['PHPuser'])) {  // This checks if the `PHPuser` sent is set
        $user = $_POST['PHPuser'];
        $time = time();
        $mysql = new mysqli($server, $username, $password, $database);
        if (!mysqli_connect_errno()) {   // Checks if connection was successful
            if ($mysql->query("INSERT INTO $table (ID, User, Timestamp)
            VALUES (NULL, $user, $time)")) {  // Checks if insert was successful
                echo '1';            // Sends '1' to the javascript if it worked
            } else {
                echo '0';            // Sends '0' to the javascript if it failed
            }
            $mysql->close();         // Closes connection to database
        } else {
            echo '0';                // Sends '0' to the javascript if it failed
        }

    }
?>

那是我的。第二行声明变量
$user
,并将其设置为从javascript发送的
PHPuser
变量的值。如果变量中已经有用户,请修改此选项,或者使用如上所述的IP地址。第三行声明变量
$time
,并将其设置为当前时间戳。第四行连接到数据库。用MySQL的这四个值替换
$server
$username
$password
$database
。第五行检查连接中的错误。如果没有错误,则第六行和第七行将该行添加到数据库中,并检查它是否工作。如果是,则会将“1”发送回javascript。如果没有,则发送“0”。最后,MySQL连接被关闭。通过一些修改来设置用户,例如,如果您在站点上的其他PHP脚本上有用户名,则设置包含用户名,或者使用IP地址,这应该可以在您的站点上使用。顺便说一下,我使用mysqli是因为mysql函数被认为是不推荐使用的,不应该使用。

好的,我知道该怎么做了。你试过什么?同意。。。一个小说明java脚本是客户端php服务器端您想在您的服务器上调用一个php脚本,并在按下按钮时调用javascript函数,您如何跟踪用户?有身份证吗?饼干?等等?基本上,我在跟踪用户。他们已经登录到一个站点,当他们单击链接下载文件时,我想记录它。好的,那么您可以在某个会话变量中访问他们的用户ID吗?谢谢。看来我现在可以完成了。谢谢你的帮助。我知道你只是提供了一个基本的例子,但如果你要回答某人,至少要提供基本的数据库安全性……使用mysql_*不推荐的函数和未替换的用户输入……不太好。
   <a href="adownloadlink.pdf" class='pdf' >YOUR LINKS</a>
   <a href="anotherdownloadlink.pdf" class='pdf' >YOUR LINKS</a>
   <a href="anewdownloadlink.pdf" class='pdf'>YOUR LINKS</a>
     $conn = new PDO("mysql:host='yourhost';dbname='yourdb'","yourusername","yourpassword");

     if(isset($_POST)){

        $id = $_POST['id'];
        $page = $_POST['page'];
        $sql = "INSERT INTO YourTable (userid, page) VALUES (:user_id, :page)";
        $q = $conn->prepare($sql);
        $test = $q->execute(array(':user_id'=>$id, ':page'=>$page));
        if($test) return "Success";

     } 
<html>
    <head>
        <title>AJAX and MySQL Demonstration</title>
        <script type="text/javascript"><script>      <!--Your script goes here-->
    </head>
    <body>
        <a href="www.example.com" id="link">Link</a> <!--This is the link-->
    </body>
</html>
$('#link').click(function() {       // Some jQuery for the on click event handler
    var user = "Paul";              // Let's treat this as the user for now
    $.post('StoreinDatabase.php', {PHPuser: user}, function(result) {
        if (result == '1') {        // Result will be '1' if it worked
            // Put code here for what to do if storage in the database worked
        } else {
            // Put code here for what to do if it failed
        }
    });
});
<?php
    if (isset($_POST['PHPuser'])) {  // This checks if the `PHPuser` sent is set
        $user = $_POST['PHPuser'];
        $time = time();
        $mysql = new mysqli($server, $username, $password, $database);
        if (!mysqli_connect_errno()) {   // Checks if connection was successful
            if ($mysql->query("INSERT INTO $table (ID, User, Timestamp)
            VALUES (NULL, $user, $time)")) {  // Checks if insert was successful
                echo '1';            // Sends '1' to the javascript if it worked
            } else {
                echo '0';            // Sends '0' to the javascript if it failed
            }
            $mysql->close();         // Closes connection to database
        } else {
            echo '0';                // Sends '0' to the javascript if it failed
        }

    }
?>
if (!empty($_SERVER['HTTP_CLIENT_IP'])) // The preferred way to get IP address
{
  $user=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) // If that isn't available
{
  $user=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else                 // If neither of the last two are available, use this way            
{
  $user=$_SERVER['REMOTE_ADDR'];
}