phppup Posted August 16, 2022 Share Posted August 16, 2022 (edited) I have added a field using JavaScript but it will not post. <form method="POST" action=""> <input type="submit"> </form> <script> const input = document.createElement("input"); input.setAttribute("type", "text"); input.setAttribute("name", "name"); input.setAttribute("value", "value"); document.body.appendChild(input); </script> Am I missing a certain command? Edited August 16, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/ Share on other sites More sharing options...
kicken Posted August 16, 2022 Share Posted August 16, 2022 14 minutes ago, phppup said: document.body.appendChild(input); You need to append the input to the <form>, not the <body>. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/#findComment-1599477 Share on other sites More sharing options...
mac_gyver Posted August 16, 2022 Share Posted August 16, 2022 you are appending the dynamically created field to the document body, not to the form. i recommend that you do two things - instead of building the markup, line by line, attribute by attribute, put the desired markup inside a <div id='template'>...</div>. you can then just copy the innerHTML from this template element to crate each new element. put another <div id='target'></div> inside the form where you want to append the dynamically created elements. the javascript would them look like this - // create an empty div element var divx = document.createElement('div'); // get the template html and put it into the empty div divx.innerHTML = document.getElementById('template').innerHTML; // append the new div to the target area on the page document.getElementById('target').appendChild(divx); Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/#findComment-1599478 Share on other sites More sharing options...
phppup Posted August 17, 2022 Author Share Posted August 17, 2022 (edited) I knew it was something like this. So I don't need to indicate a form of, just the form tag is enough? @mac_gyver I like that idea. And the template can essentially be anywhere on the <body> because the placement is handled by "target", right? Although they do need independent names for submission, don't they? Edited August 17, 2022 by phppup Forgot item Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/#findComment-1599483 Share on other sites More sharing options...
mac_gyver Posted August 17, 2022 Share Posted August 17, 2022 if you are dynamically adding form fields, you should probably be using an array name for the fields, so that you will get an array of submitted data from the set of fields. Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/#findComment-1599484 Share on other sites More sharing options...
phppup Posted August 17, 2022 Author Share Posted August 17, 2022 I was thinking that would be a solution. So I guess I'm on the right path, but I may just go the long route since it's only for a few fields. Quote Link to comment https://forums.phpfreaks.com/topic/315193-adding-a-field-to-form/#findComment-1599485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.