One of my div dynamically outputs this:
style="position: relative; left: 77px; top: -14px;"
while i use dragging.
How can i save this in a php variable and store it in the database.
One of my div dynamically outputs this:
style="position: relative; left: 77px; top: -14px;"
while i use dragging.
How can i save this in a php variable and store it in the database.
Here is one way of doing it. But please note that this uses jQuery, jQuery UI and a PHP page to handle and save the data. In other words, this is an inefficient mess. Someone should be coming up with a better way shortly.
This answer uses code
from this answer, thanks to Simen Echholt: https://stackoverflow.com/a/4139860/3186769
To see if the div
was dragged, we first check if there is a mousedown
event on the div
. If there is, then we check if there is a mousemove
event on it before there is a mouseup
. If there is, then the div
has been dragged.
We post the left
and top
position of the div
if it has been dragged. Here is the javascript to implement this:
// For the sake of simplicity, let us assume the div had an id "d".
$(function() {
var isDragging = false;
$("#d")
.mousedown(function() {
$(window).mousemove(function() {
isDragging = true;
$(window).unbind("mousemove");
});
})
.mouseup(function() {
var wasDragging = isDragging;
isDragging = false;
$(window).unbind("mousemove");
if (wasDragging) {
var left = $("#d").position().left;
var top = $("#d").position().top;
$.post("backend.php", {left: left, top: top});
}
});
$("#d").draggable(); // allow the div to be dragged.
});
Here is an example of what backend.php
could look like. This is the PHP page that our javascript is posting to.
<?php
if (isset($_POST['left']) && isset($_POST['top']))
{
$db = new mysqli("localhost", "test", "test", "test") or die("This error message will not be visible on your HTML page unless you add a function in the jQuery post method to handle the returned output");
$res = $db->query("INSERT INTO rememberPosition (left, top) values (" . $db->escape_string($_POST['left']) . ", " . $db->escape_string($_POST['top']) . ")");
$db->close();
}
?>
The HTML for the div is pretty simple, and it should look something like this:
<div id="d" style="<?php require('memory.php'); echo $styleString; ?>">Here is a div with super-powers... It can actually fly around :)</div>
where we load the saved position values in memory.php
. Here is an example of what memory.php
could look like:
<?php
$styleString = "position: relative; left: 0px; top: 0px;";
$db = new mysqli("localhost", "test", "test", "test") or die("Error connecting to database.");
if ($res = $db->query("SELECT left, top FROM rememberPosition"))
{
if ($row = $res->fetch_assoc())
{
$styleString = "position: relative; left: " . $row['left'] . "px; top: " . $row['top'] . "px;";
}
}
$db->close()
?>
Hope that helps :)
You should wait for a better method, before using this one.
You should add jQuery and jQuery UI for this example to work.
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
Any of the below will work
$var = "style=\"position: relative; left: 77px; top: -14px;\"";
$var = 'style="position: relative; left: 77px; top: -14px;"';
$var = addslashes('style="position: relative; left: 77px; top: -14px;"');
That last one is really pointless, but I figured I'd put it there for reference. I'd suggest the middle option as the best way.
The database insertion will take care of any necessary escaping, assuming you use prepared. statement.