[英]Drag and Drop(event) not trigger ajax POST
I have three pages: test.php, script.js, and main.php. 我有三个页面:test.php,script.js和main.php。
Main.php is using html5 drag and drop along with a simple ajax script from script.js, in an effort to post to- and activate test.php. Main.php正在使用html5拖放以及来自script.js的简单ajax脚本,以便发布并激活test.php。 (side note, i would like main.php to pass the
<img id="s1" />
as a POST variable. After several hours of research and a hundred or so tests and revisions I am unable to figure out why I cannot get ondrop to trigger the post. Any advice would be greatly appreciated. Here is my code: (旁注,我希望main.php将
<img id="s1" />
作为POST变量传递。经过几个小时的研究和大约一百次测试和修改,我无法弄清楚为什么我无法得到ondrop触发帖子。非常感谢任何建议。这是我的代码:
test.php (contains a simple php script that when loaded inserts a generic record into my DB) test.php(包含一个简单的php脚本,加载时将通用记录插入我的DB)
script.js 的script.js
function drop(id, event) {
$.ajax({
url: "test.php",
type: "POST",
data: {
id: id,
event: event
},
success: function () {
console.log('great success');
return true
}
});
return false;
}
and main.php 和main.php
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/script.js"></script>
<header class="main-header" role="banner"><center>
<img src="lampettalogo.jpg" height="90" width="400"alt="Banner Image"/></center>
</header>
<style>
#1 {width:auto;height:auto;padding:1px;border:1px solid #aaaaaa;}
#2 {width:auto;height:auto;padding:1px;border:1px solid #aaaaaa;}
#3 {width:auto;height:auto;padding:1px;border:1px solid #aaaaaa;}
#4 {width:auto;height:auto;padding:1px;border:1px solid #aaaaaa;overflow: auto;}
</style>
</head>
<body>
<div id="1" ondrop="drop(event)" ondragover="allowDrop(event)">
<?php
include "database_connection.php";
///////////////////////////////////////////////////////////////////////////////////////////////
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else
{
}
$query = "SELECT * FROM ss where currentZone = 1";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "<img id='{$row["sID"]}' src='{$row["photoLink"]}.jpg' draggable='true' ondragstart='drag(event)' width='75' height='75'>" ;
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
/////////////////////////////////////////////////////////////////////////////////
?>
</div>
<div id="2" ondrop="drop(event)" ondragover="allowDrop(event)">
<?php
include "database_connection.php";
$query = "SELECT * FROM ss where currentZone = 2";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "<img id='{$row["sID"]}' src='{$row["photoLink"]}.jpg' draggable='true' ondragstart='drag(event)' ondrop='drop(event)' width='75' height='75'>" ;
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
?>
</div>
<div id="3" ondrop="drop(event)" ondragover="allowDrop(event)">
<?php
include "database_connection.php";
$query = "SELECT * FROM ss where currentZone = 3";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "<img id='{$row["sID"]}' src='{$row["photoLink"]}.jpg' draggable='true' ondragstart='drag(event)' width='75' height='75'>" ;
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
?>
</div>
<div id="4" ondrop="drop(event)" ondragover="allowDrop(event)">
<?php
include "database_connection.php";
$query = "SELECT * FROM ss where currentZone = 4";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "<img id='{$row["sID"]}' src='{$row["photoLink"]}.jpg' draggable='true' ondragstart='drag(event)' width='75' height='75'>" ;
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
?>
</div>
<div id="4" ondrop="drop(event)" ondragover="allowDrop(event)">
<?php
include "database_connection.php";
$query = "SELECT * FROM ss where currentZone = 0";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo "<img id='{$row["sID"]}' src='{$row["photoLink"]}.jpg' draggable='true' ondragstart='drag(event)' width='75' height='75'>" ;
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
?>
</div>
</body>
</html>
In your drop(id, e)
method, you can consider the following, in addition to your allowDrop
method. 在
drop(id, e)
方法中,除了allowDrop
方法之外,还可以考虑以下内容。 Use the FileReader
class to read your file. 使用
FileReader
类读取您的文件。
function drop(id, e) {
if (e.dataTransfer && e.dataTransfer.files.length != 0) {
var file = e.dataTransfer.files[0], // Only the first file.
reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (event) {
console.log(file.name);
$.ajax({
url: "test.php",
type: "POST",
data: {
id: id,
fileName: file.name, // Your file name.
file: event.target.result // Your file.
},
success: function () {
console.log('great success');
return true
}
});
};
}
}
And in your HTML, you need to pass a value in id
as well. 在HTML中,您还需要传递
id
的值。 For example, you can do the following to print out your $row["sID"]
into the method parameter. 例如,您可以执行以下操作将
$row["sID"]
打印到method参数中。
<div id="1" ondrop="drop('<?php echo $row["sID"]; ?>', event)" ondragover="allowDrop(event)">
On your PHP script, you need to be able to receive the POST
ed file. 在PHP脚本上,您需要能够接收
POST
ed文件。 An example is shown below. 一个例子如下所示。
$data = $_POST['file'];
$fileName = $_POST['fileName'];
$id = $_POST['id'];
$serverFile = $fileName . "-" . time(); // Appends timestamp so that files of the same name wouldn't be overwritten.
$fp = fopen('/uploads/' . $serverFile, 'w');
fwrite($fp, $data);
fclose($fp);
$returnData = array( "serverFile" => $serverFile );
echo json_encode($returnData);
See this plunker for an example. 有关示例,请参阅此plunker 。 Drop a file into the div, and watch the console log.
将文件拖放到div中,然后观察控制台日志。
Edit 编辑
Understood that you want to drag and drop elements instead. 理解你想要拖放元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.