我有三个页面:test.php,script.js和main.php。
Main.php正在使用html5拖放以及来自script.js的简单ajax脚本,以便发布并激活test.php。 (旁注,我希望main.php将<img id="s1" />
作为POST变量传递。经过几个小时的研究和一百次左右的测试和修改后,我无法弄清楚为什么我无法触发触发任何建议都将不胜感激。这是我的代码:
test.php(包含一个简单的php脚本,加载后将通用记录插入我的数据库)
的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;
}
和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>
答案 0 :(得分:6)
在drop(id, e)
方法中,除allowDrop
方法外,您还可以考虑以下方法。使用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
}
});
};
}
}
在HTML中,您还需要在id
中传递值。例如,您可以执行以下操作将$row["sID"]
打印到方法参数中。
<div id="1" ondrop="drop('<?php echo $row["sID"]; ?>', event)" ondragover="allowDrop(event)">
在PHP脚本上,您需要能够接收POST
ed文件。示例如下所示。
$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);
有关示例,请参阅此plunker。将文件放入div,然后观察控制台日志。
修改强>
理解你想要拖放元素。
以下是更新的plunker。