For some context, I have have three divs for dragging and dropping items. The first div will be the default div to start things off. Then I want the second two divs to update the database when I drop content in there. These divs will be dynamic and I may have 5 or 6 divs at any give time, but also could only have 2 or 3. I am NOT very experienced with javascript. I have gotten this far, but am struggling with getting further. This is a three part question:
A) How do I provide a sum PER DIV - Right now the sum div sums up no matter which div I drop the element into. I'm also looking to subtract from the sum when an object leaves the div as well as delete the entry from the database (NOT INCLUDING THE ORIGINAL FIRST DIV).
B) How do I send multiple $_POST values to update.php using an ajax request? I've tried data:{data: data, name: name, check: check, amount: amount},
, but that makes my sum div stop working. If I run it as is, I'm just getting the "amount" value (which is the div id), but it's not posting as 'amount'. I'm trying to get the div id of the dropped element(amount), the div content of the dropped element (the name) and the div id that the object was dropped into, or the parent div's id (the check). As I said before this javascrip/ajax thing is new to me. I'm not finding what I'm looking for after searching for hours.
C) Is there a way to return the 'total' variable to my script FROM update.php instead of adding things up inside of the script?
javascript
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
var total = 0;
function drop(ev, ui) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
$.ajax({
type:'POST',
url:'../update.php',
data:{data:data},
success: function(){
total += parseFloat(data);
$('#sum').html(total);
}
});
}
html
<h2>Drag and Drop</h2>
<div id="center">
<div id="2018-08-01" ondrop="drop(event)" ondragover="allowDrop(event)">
<div class="move" draggable="true" ondragstart="drag(event)" id="1056.23">Mortgage</div>
<div class="move" draggable="true" ondragstart="drag(event)" id="10">Fees</div>
</div>
<div id="2018-08-05" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="sum">Sum Here</div>
<div id="2018-08-15" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="sum">Sum Here</div>
</div>
update.php
$name = $_POST['name'];
$check = $_POST['check'];
$amount = $_POST['amount'];
$sql = "INSERT INTO bills (bills_id, bills_name, bills_check, bills_amount) VALUES ('', '$name', '$check', '$amount')";
mysqli_query($connect, $sql);