두 개의 입력을 고유 한 ID (i ++ 및 r ++ from original)와 복제하는 버튼이 있습니다. 내가 양식을 제출하고 실행하면 :입력 복사본이있는 제출 양식
foreach ($_POST as $id => $value) {
echo $value;
}
process.php 페이지는 새로운 입력 $ ID 또는 $ 값을 인식하지 못합니다.
중복 코드는 다음과 같다 :
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>
사용자는 자신이 원하는만큼의 중복을 사용할 수있는 가능성을 가지고 있어야합니다. 각각은 서로 다른 입력을 가지므로 mysql에서 고유 ID를 변경해야하는 이유가 있습니다.
새 ID가 표시되거나 인식되지 않는 이유와 중복 된 입력에서 모든 값을 가져 오는 더 좋은 방법이 있습니까?
당신이 다루는 html을 보여주지 않았기 때문에 우리는 도울 수 없습니다. PHP는 JS 코드가 무엇인지 신경 쓰지 않을 수 있습니다. 양식 제출시 브라우저가 HTML을 변환하는 것을 볼 수 있습니다. –
jquery를 먼저 사용하는 것이 좋습니다. 작업하기가 더 쉽습니다. jquery를 사용하여 serializeArrays 함수를 사용하여 폼의 값을 얻은 다음 AJAX 요청을 사용하여 서버로 보냅니다. – Nicolas