[英]How to dynamically create form inputs and then submit them to php
I have a page where the user can press a button that will append two new form inputs to the page. 我有一个页面,用户可以在其中按一个按钮,该按钮会将两个新的表单输入追加到该页面。 The user can do this as many times as necessary.
用户可以根据需要多次执行此操作。 The problem I am having is that I cannot seem to access the new inputs in the php file I am submitting to.
我遇到的问题是我似乎无法访问要提交的php文件中的新输入。
Here is the code for the button on the page the user sees. 这是用户看到的页面上按钮的代码。 It is within a form.
它在表格内。
<div class='instruction'>
<p>Please use the checkpoint tool to outline the primary steps
necessary for completion of the project.
<h4 style='margin-bottom:5px;'>Checkpoint Tool</h4>
<input type='button' onclick='addCheckpoint()' value='Add Checkpoint' />
<input type='text' id='count' name='projectCount' style='width:25px;' readonly='true' />
</p>
<div id='checkpoints'>
</div>
</div>
Here is the javascript function addCheckpoint() 这是JavaScript函数addCheckpoint()
function addCheckpoint()
{
var count = +document.getElementById('count').value;
// Declare Variables
var checkpointText = "Checkpoint "+(count+1);
var nameText = "Checkpoint Name: ";
var instructionText = "Instructions: ";
var previous = document.getElementById("checkpoints");
var newP = document.createElement("p");
var nameInput = document.createElement("input");
nameInput.setAttribute("type","text");
nameInput.setAttribute("name","checkpointName" + count);
var instructionInput = document.createElement("textarea");
instructionInput.setAttribute("name","checkpointInstruction" + count);
instructionInput.setAttribute("rows","4");
instructionInput.setAttribute("cols","56");
// Append Variables
newP.appendChild(document.createTextNode(checkpointText));
newP.appendChild(document.createElement("br"));
newP.appendChild(document.createElement("br"));
newP.appendChild(document.createTextNode(nameText));
newP.appendChild(nameInput);
newP.appendChild(document.createElement("br"));
newP.appendChild(document.createTextNode(instructionText));
newP.appendChild(instructionInput);
newP.appendChild(document.createElement("br"));
newP.appendChild(document.createElement("hr"));
previous.appendChild(newP);
document.getElementById('count').value = count + 1;
}
Here is the code on the page being submitted to that tries to retrieve the posted values. 这是提交到页面上的代码,该代码试图检索发布的值。
$projectCount = strip_tags($_POST["projectCount"]);
for($i = 0; $i < $projectCount; $i += 1)
{
$checkpointNames[] = strip_tags($_POST["checkpointName".$i]);
$checkpointDescriptions[] = strip_tags($_POST["checkpointDescription".$i]);
}
I appreciate any help that can be offered! 感谢提供的任何帮助!
Ok, I figured it out, it was a rookie mistake. 好的,我知道了,这是一个菜鸟错误。 My inputs weren't even being appended to my form.
我的输入甚至没有附加到我的表单中。 Thanks for the help though!
谢谢您的帮助! I appreciate the time you gave to look over my code.
感谢您抽出宝贵的时间查看我的代码。
尝试将元素放入中,并确保使用提交按钮或javasrtipt的form.submit提交表单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.