Reputation: 834
I have three pages: test.php, script.js, and 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. (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:
test.php (contains a simple php script that when loaded inserts a generic record into my DB)
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
<!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>
Upvotes: 4
Views: 1763
Reputation: 1423
In your drop(id, e)
method, you can consider the following, in addition to your allowDrop
method. Use the FileReader
class to read your file.
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. For example, you can do the following to print out your $row["sID"]
into the method parameter.
<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. 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. Drop a file into the div, and watch the console log.
Edit
Understood that you want to drag and drop elements instead.
Below is the updated plunker.
Upvotes: 6