/    Sign up×
Community /Pin to ProfileBookmark

Drag and Drop Form Elements

I’ve got a page which has a form on it. Inside the form tags I have a DIV for a drop zone.

I’m dragging DIV’s from outside of the form tags into this drop zone. Inside these draggable DIV’s are input elements containing values.

My console log shows the ID’s of these draggable DIV’s after they’ve been dropped into the drop zone.

When I inspect my code, the input element shows inside the the form before being posted but when I post the form I don’t get these input values.

`var_dump($_POST);`

I’m using this code
https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop

This is my testing mods.

I assume it’s something to do with the inputs not being in the form when it’s loaded but is there anything I can do or do I need to loop through and add ID’s to a textbox already in the form??

“`
function drag(ev) {
ev.dataTransfer.setData(“text”, ev.target.id);
}

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData(“text”);
ev.target.appendChild(document.getElementById(data));
var n = ev.target.childNodes;
n.forEach(function(item){
if (item.id != undefined){
console.log(item.id)
}
});
}
“`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJan 04.2022 — try moving <form> to right after <body>

and

move </form> to right before </body>

> @kiwis80#1641121 I assume it's something to do with the inputs not being in the form
Copy linkTweet thisAlerts:
@SempervivumJan 04.2022 — @kiwis80#1641121 I tested this and it worked fine: Values of the inputs having been dragged into the form were sent to the PHP script. Did you assign names to the inputs being dragged and dropped? If this is not the reason for this issue please post the HTML of your form and the div being dragged.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...