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

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!

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题