duanfei7508 2012-05-02 20:15
浏览 37
已采纳

想要在退出时与服务器连接。 我可以设置任何形式的Javascript / jQuery / AJAX陷阱吗?

First things first. I'm running CodeIgniter on a PHP/MySQL site.

I've been tasked with creating a "shell" application for a series of movies that a person can view for a training website that I'm building. The idea here is that a person would be able to log on to a page, click "Take Course" and have it log what time the course was taken. Then, when the person exits the page carrying the movie, I would be able to record what time the course was ended. This way I can compare the start and end times, and determine if the user had viewed most of the film in order to give the user credit. Well, the first part is easy. First, I've built a table in my database with the following fields:

  • intKey (intKey(10))
  • strHash (varchar(255))
  • dtBegan (datetime)
  • dtEnded (datetime)
  • varIpAddress (varchar(255))

I've put in a controller that does this:

$ip_address = $_SERVER['REMOTE_ADDR'];
$data['hash'] = md5(time() . "Proof of Concept!");
$this->db->query("INSERT INTO pocTime SET strHash = '" . $data['hash'] . "', dtBegan = now(), varIpAddress='$ip_address'");
$this->load->view('welcome_message',$data);

OK... easy enough, yes? I also know that when I am done, I want to launch a file that does this:

$ip_address = $_SERVER['REMOTE_ADDR'];
$this->db->query("UPDATE pocTime SET dtEnded = now() WHERE strHash = '" . $data['hash'] . "' AND varIpAddress='$ip_address'");

What I'm hoping to do is have a page that is called by the 1st block of code, and set a "trap" so to speak so that the 2nd block of code is run by some kind of "post back" if the rendered page is closed. Any suggestions? I know that I could put some sort of big "CLICK HERE TO END YOUR VIDEO" type button that would cover this, but I'm looking to run code on some form of exit from a page, because I don't know what the user would do after viewing the video. Any thoughts?

  • 写回答

3条回答 默认 最新

  • dou6495 2012-05-02 20:19
    关注

    Simple but should work, using jQuery

    $(window).unload(function(){
        $.get("test.php?dim1=turn&dim2=off");
    });
    

    Using unload which jQuery documentation tells what triggers this: http://api.jquery.com/unload/

    Using get which jQuery documentation is here: http://api.jquery.com/get/

    Or you can use post as well

    $(window).unload(function(){ 
        $.post("test.php", { dim1: "turn", dim2: "off" } ); 
    });
    

    http://api.jquery.com/jQuery.post/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝