doushihu5475 2017-07-11 07:45
浏览 39

jQuery拖放 - 将位置保存到MysQL数据库中

I have been creating a small map with different positions for a fabric. My goal is to make sure I can drag 1, 2, 3 and 4 (persons) into a position (e.g. Production Leader) and save the position of the DIV into the MySQL Database. I want a global map so each person which visit this page will see the same.

I created different DIV's for each person (1, 2, 3 and 4) which are already in the database.

I'm stuck right now.. can somebody help me?

Fiddle: https://jsfiddle.net/fj1zgw2o/

Database connection and showing persons from the database:

function choosePerson() {
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, name, image, position FROM Persons";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo '<div class="boxPersons posLeft" id="'. $row['id'] .'">'. $row['id'] .'</div>';
    }
} else {
    echo "0 results";
}
}
  • 写回答

1条回答 默认 最新

  • douquan9826 2017-07-11 08:23
    关注

    // Move the box into the corresponding id.
    // When you load your boxPersons have each 
    // person an id column that matches what position they will be in.
    $(".boxPersons").each(function(e){
      var target = $(this).attr("data-id");
      $("#"+target).append($(this));
    });
    
    $(".boxPersons").draggable({
      revert: 'invalid',
      containment: '.box',
      cursor: 'move'
    });
    
    $(".openSpots, .box-persons").droppable({
      accept: ".boxPersons",
      drop: function(event, ui) {
        var droppable = $(this);
        var draggable = ui.draggable;
        // Move draggable into droppable
        draggable.appendTo(droppable);
        draggable.css({
          top: '0px',
          left: '0px'
        });
      }
    });
    .box-content {
      display: flex;
    }
    
    .toPlan {
      width: 10%;
    }
    
    .overviewPlanning {
      flex: 1;
    }
    
    .box-persons {
      overflow: hidden;
      border-radius: 4px;
      border: 1px solid #d8d8d8;
    }
    
    .boxPersons {
      width: 60px;
      height: 72px;
      padding: 5px;
      margin: 10px;
      text-align: center;
      border: 1px solid #d8d8d8;
      border-radius: 4px;
      z-index: 99;
      float: left;
      background: #888;
    }
    
    .posLeft {
      float: left;
    }
    
    .openSpots {
      width: 60px;
      height: 72px;
      padding: 5px;
      margin: 10px;
      text-align: center;
      border: 0.5px dashed #000000;
      border-radius: 4px;
      z-index: 1;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
    <header>
      Header
    </header>
    
    <div class="box">
      <div class="box-content">
        <div class="toPlan">
          <div class="productionLeader">
            <strong>Production Leader</strong>
            <div id="Leader" class="openSpots" id="openSpots">
    
            </div>
            <strong>Free</strong>
            <div id="Free" class="openSpots positionFree">
    
            </div>
            <strong>Ill</strong>
            <div id="Ill" class="openSpots positionIll">
    
            </div>
            <strong>Otherwise</strong>
            <div id="Otherwise" class="openSpots positionOtherwise">
    
            </div>
          </div>
        </div>
        <div class="overviewPlanning">
          Fabric map
        </div>
      </div>
      <div class="box-persons">
        Available collegues (to drag and drop into a spot)<br>When you load the data into this box change the id to match what was saved. You can use AJAX to keep everything synced.<br>
        <div class="boxPersons" data-id='Free'>bob</div>
        <div class="boxPersons" data-id='Ill'>marry</div>
        <div class="boxPersons" data-id=''>mark</div>
      </div>
    </div>
    
    <footer>
      Footer
    </footer>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图