我有一个按钮,它们用自己的id(i ++和r ++从原始)复制两个输入。当我提交表单并运行时:输入重复的提交表格
foreach ($_POST as $id => $value) {
echo $value;
}
process.php页面无法识别新输入$ id或$ value。
的重复代码如下:
document.getElementById('button').onclick = duplicate
var i = 0;
var r = 0;
var items = document.getElementById('items');
var cantidad = document.getElementById('cantidad');
function duplicate() {
var cloneitems = items.cloneNode(false); // "deep" clone
cloneitems.id = "items" + ++r; // there can only be one element with an ID
items.parentNode.appendChild(cloneitems);
var cloneCantidad = cantidad.cloneNode(false); // "deep" clone
cloneCantidad.id = "cantidad" + ++i; // there can only be one element with an ID
cantidad.parentNode.appendChild(cloneCantidad);
}
我的HTML:
<form method="post" action="process.php" enctype="multipart/form-data">
<h4 class="input-margin">Items<button type="button" id="button" onclick="duplicate()" class="btn btn-primary margin-left input-margin">+</button></h4>
<div>
<div class="input-margin">
<div class="col-xs-12 col-sm-6">
// First duplicated input
<select class="input-margin form-control" id="items" placeholder="Nombre" name="item" data-style="btn-primary">
<option></option>
<?php while($row = mysqli_fetch_array($itemResults)) : ?>
<option><?php echo $row['item']; ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-xs-12 col-sm-6">
// Second duplicated input
<input type="number" class="input-margin form-control txtboxToFilter" id="cantidad" placeholder="Cantidad" name="cantidad" min="1">
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 submit-button">
<button type="submit" class="btn btn-primary btn-block" name="submit" value="ingreso">Ingresar</button>
</div>
</form>
用户必须有,因为他们想用尽可能多的重复的可能性。他们每个人都会有不同的输入,这就是为什么我需要唯一的ID来改变MySQL。
为什么新的ID没有被显示或识别,并且有没有更好的方法从重复的输入中获取所有的值?
由于您没有显示您正在摆弄的HTML,所以我们无法提供帮助。 PHP并不关心JS代码是什么 - 它只会在提交表单时看到浏览器将HTML变成什么。 –
我会建议先使用jquery,它的方式更容易。使用jquery,我会使用serializeArrays函数来获取表单的值,然后使用AJAX请求将其发送到您的服务器。 – Nicolas