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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效