如何将对图片的点击记录到mysql数据库?可以使用函数onComplete或Ajax吗?
发布于 2011-12-27 23:14:11
您可以为Fancybox 2公开的多个回调事件之一添加回调。在本例中,我将使用afterLoad (如果您使用的是Fancybox 1.X,那么onComplete将是一个很好的绑定事件):
$('.my-links').fancybox({
afterLoad : function () {
$.get('path/to/server-side-script.php', { id : this.orig[0].id }, function (response) {
//you can now access the server response via the `response` variable
});
}
});注意:上面的代码片段假设每个图像都有自己的ID,否则需要将this.orig[0].id更改为每个图像的一些独特的内容。
PHP脚本应该如下所示:
<?php
if (isset($_GET['id'])) {
//connect to DB here
$query = mysql_query("INSERT INTO tracking_table (image_id) VALUES (" . mysql_real_escape_string($_GET['id']) . ")", $db) or trigger_error(mysql_error());
}
?>当然,这需要您用PHP编写服务器端代码。
https://stackoverflow.com/questions/8649896
复制相似问题