duanjingwei7239 2016-09-13 08:45
浏览 146
已采纳

HTML表单按用户会话锁定

I have created a web site that allows for the submission of HTML forms with multiple stages of form authorisation. For example a Employee Submits a Leave Application which is then Authorised by a Supervisor. Multiple users may have the permission to authorise a form at any particular stage.

The issue i have run into is only 1 user should be able to edit the form at any stage.

My idea was on page load set a IsLocked column in the database to true when the page loads and i can use this to see if anyone else has it locked. I would then set it back to 0 when the person leaves the page. A person can leave the page by modifying the form and clicking submit or just by navigating away.

I can easily lock the page or check if its locked using the document.ready function and a ajax request or doing it straight in PHP however i am stuck in finding a way to remove the lock when the page gets redirected.

I have looked into the following methods:

  1. http://www.bennadel.com/blog/1520-binding-events-to-non-dom-objects-with-jquery.htm (Using a custom function which constantly checks to see if the href has changed. I can't seem to get this function to work in Chrome, Firefox, IE)
  2. https://developer.mozilla.org/en-US/docs/Web/Events/popstate (The problem us it only called when the forward or back buttons in web browser are used)

Question: How can l reliably get when a user has left the page? Is there a better way to handle the form locking mechanism?

  • 写回答

2条回答 默认 最新

  • douhuan4699 2016-09-13 09:01
    关注

    I would recommend WebSockets. They provide real-time communication between connected clients to a server. This could solve your problem by broadcasting a message to all user's about which forms are locked.

    You have a few options for the websocker server, a popular one being NodeJs. Take a look at this answer for the various libraries available.

    Once your server is running, each client could broadcast a message to the other clients with the form that needs to be locked:

    // Send text to all users through the server
    function sendText() {
      // Construct a msg object containing the data the server needs to process the message from the client.
      var msg = {
        type: "message",
        text: document.getElementById("myLockedForm").id,
        id:   clientID,
        date: Date.now()
      };
    
      // Send the msg object as a JSON-formatted string.
      exampleSocket.send(JSON.stringify(msg));
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵