如何使用ajax在函数拖放javascript中调用php函数

如何使用ajax在函数拖放javascript中调用php函数,javascript,php,ajax,Javascript,Php,Ajax,我必须做一个项目,可以处理文件系统(上传文件,下载,删除,重命名,移动文件夹中的文件等…)。 所以我用javascript在另一个目录中拖放了一个文件,但我想在每次传输中调用php函数来更新数据库中的文件夹等等。。。我该怎么办?这里是我的拖放代码: window.onload = function () { function drag_and_drop() { for (var items = document.querySelectorAll('[data-draggable="ite

我必须做一个项目,可以处理文件系统(上传文件,下载,删除,重命名,移动文件夹中的文件等…)。 所以我用javascript在另一个目录中拖放了一个文件,但我想在每次传输中调用php函数来更新数据库中的文件夹等等。。。我该怎么办?这里是我的拖放代码:

window.onload = function () {
function drag_and_drop() {
    for (var items = document.querySelectorAll('[data-draggable="item"]'), len = items.length, i = 0; i < len; i++) {
        items[i].setAttribute('draggable', 'true');
    }

    /* DRAG N DROP */
    var item = null;

    document.addEventListener('dragstart', function (e) {
        item = e.target;
        e.dataTransfer.setData('text', '');

    }, false);
    document.addEventListener('dragover', function (e) {
        if (item) {
            e.preventDefault();
        }

    }, false);

    document.addEventListener('drop', function (e) {
        if (e.target.getAttribute('data-draggable') == 'target') {
            e.target.appendChild(item);
            e.preventDefault();
           /*call a php function here*/ 


        }

    }, false);

    document.addEventListener('dragend', function (e) {
        item = null;

    }, false);

}

drag_and_drop();
window.onload=函数(){
函数拖放(){
对于(var items=document.querySelectorAll('[data draggable=“item”]”),len=items.length,i=0;i

}

您可以使用jQuery Ajax方法调用php文件,并通过POST为其提供参数

移动文件的示例如下所示:

$.ajax({   
  type: "POST",   
  url: "/destination/to/php/file.php",   
  data: "sourceFile=" + sourcefile + "&destinationFile=" + destinationFile, 
  success: function(data) {
    // Request succeded, data is the string containing the result returned by the php file. This could be an error message or something else. You can also use JSON to have multiple values.
    // Decode json
    decodedData = JSON.decode(JSON.parse(data));
    if (decodedData == "success") {
      console.log("Moved file");
    } else {
      console.log("Error while moving file: " + decodedData);
    }
  },
  fail: function() {
    console.log("Request failed");
  }
});
然后在PHP文件中,您只需读取post值,执行操作并返回消息。我建议用JSON对消息进行解码/编码

<?php
  $destinationFile = $_POST["sourceFile"];
  $sourceFile = $_POST["destinationFile"];
  // Change files in the Database

  if ($suceeded) {
    die(json_encode("succeded"));
  } else {
    die(json_encode($errormessage));
  }

您可以使用jQuery Ajax方法调用php文件,并通过POST为其提供参数

移动文件的示例如下所示:

$.ajax({   
  type: "POST",   
  url: "/destination/to/php/file.php",   
  data: "sourceFile=" + sourcefile + "&destinationFile=" + destinationFile, 
  success: function(data) {
    // Request succeded, data is the string containing the result returned by the php file. This could be an error message or something else. You can also use JSON to have multiple values.
    // Decode json
    decodedData = JSON.decode(JSON.parse(data));
    if (decodedData == "success") {
      console.log("Moved file");
    } else {
      console.log("Error while moving file: " + decodedData);
    }
  },
  fail: function() {
    console.log("Request failed");
  }
});
然后在PHP文件中,您只需读取post值,执行操作并返回消息。我建议用JSON对消息进行解码/编码

<?php
  $destinationFile = $_POST["sourceFile"];
  $sourceFile = $_POST["destinationFile"];
  // Change files in the Database

  if ($suceeded) {
    die(json_encode("succeded"));
  } else {
    die(json_encode($errormessage));
  }

每次拖放时,您都可以触发一个AJAX调用,并发送一些内容,例如文件名、要调用的拖放位置。此调用将命中PHP端点,您可以从中获取这些信息。每次拖放时,您都可以触发一个AJAX调用,并发送一些内容,例如文件名、要调用的拖放位置。此调用将命中PHP端点,您可以从中获取这些信息。并在PHP中更新数据库。我成功地在PHP中使用了我的两个变量,但为什么我要使用console.log,我得到了我的两个变量和所有html这只是为了调试目的。当操作成功执行时,我只需更新UI。如果失败,我会显示类似错误警报的内容。因此,您可以使用适合您需要的内容替换console.log。我成功地在php中使用了我的两个变量,但为什么我要使用console.log,我得到了我的两个变量和所有html这只是为了调试目的。当操作成功执行时,我只需更新UI。如果失败,我会显示类似错误警报的内容。因此,您可以使用适合您需要的内容替换console.log。