douti9253 2016-01-20 15:10
浏览 36
已采纳

onclick使用Ajax

I have a web application that is using Drag/Drag capabilities for sorting data. I have discovered that on IOS devices drag and drop via safari is a no go. I'm looking for a temporary solution to allow the application to run while I work on an IOS app. I'm wondering if it is possible to use Ajax to POST on click to a dataworker page I have that processes the drag and drop actions. Here is what I have so far.

Page1.php contains

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="/js/script.js"></script>
    <header class="main-header" role="banner"><center>
</header>
  </head>
  <body>
  <center>
 <table>
 <tr><td>
 <div id="available" ondrop="drop('list', event)" ondragover="allowDrop(event)">
 <p> <font color="blue">SID</font> | Last, First </p> <?php 
include "database_connection.php";
///////////////////////////////////////////////////////////////////////////////////////////////
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}
else
{
}
$query = "*******";
if ($result = mysqli_query($link, $query)) {
    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
            echo "<p id='{$row["sID"]}' draggable='true' onclick='drop(event)' ondragstart='dragStart(event)' width='75' height='75'> <font color='blue'> {$row["sID"]}</font> |  {$row["lastName"]}, {$row["firstName"]}</p></a>";
    }
    /* free result set */
    mysqli_free_result($result);
}
 mysqli_close($link);
 /////////////////////////////////////////////////////////////////////////////////
?>

 </div>
 </td>
 <td>

 <div id="order" ondrop="drop('order', event)" ondragover="allowDrop(event)">
  <?php 
include "database_connection.php";
///////////////////////////////////////////////////////////////////////////////////////////////
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}
else
{
}
$query = "*********";
if ($result = mysqli_query($link, $query)) {
    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<p id='{$row["sID"]}' draggable='true' ondragstart='dragStart(event)' width='75' height='75'><font color='blue'>  {$row["sID"]} </font>| {$row["lastName"]}, {$row["firstName"]} </p></a>";
    }
    /* free result set */
    mysqli_free_result($result);
}
 mysqli_close($link);
 /////////////////////////////////////////////////////////////////////////////////
?>

 </div>
 </td>
</tr>

  </body>

</html>

Script.js contains

function drop(containerId, e) {
  var id = event.dataTransfer.getData("id");

  console.log(id);

  $.ajax({
    url: "dataworker.php",
    type: "POST",
    data: {
      id: id,
      containerid: containerId,
      //fileName: file.name, // Your file name.
      //file: event.target.result // Your file.
    },
    success: function() {
      console.log('great success');
      window.location.reload()
      return true
    }
  });
}

function dragStart(e) {
  console.log(e);
  e.dataTransfer.setData("id", e.srcElement.id);
}

function allowDrop(e) {
  e.preventDefault();
}

And the third page, Dataworker, simply accepts the POSTS via php and processes them. Is it possible to also have an Onclick perform the same action as drop? My hope is that can be done, and I wont have to use traditional hard coded get ahrefs to accomplish my goal. it is important to note that since they are also drag and drop if I use the same functions as the drag and drop I need to account for the containerID. That way the POST feature knows that it is actually going to the opposite container, and not the one it was clicked in.

  • 写回答

1条回答 默认 最新

  • duanmibei1929 2016-01-20 18:18
    关注

    I found the answer by adding the IOS html 5 drag and drop Shim. https://github.com/timruffles/ios-html5-drag-drop-shim. This is a hack that allows mobile browsers to register drags. credit goes to TrueBlueAussie for putting me on the right track. Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波