我使用Javascript
动态添加textbox
,以便用户在一个进程中添加更多项。以下是添加textboxes
:的代码
<script type="text/javascript">
var quantity = document.getElementById('quantity');
var item = document.getElementById('item');
var serial = document.getElementById('serial');
var property = document.getElementById('property');
document.getElementById('add').onclick = function () {
var input = document.createElement('input'),
div = document.createElement('div');
input.type = "text";
input.setAttribute("name", "quant");
div.appendChild(input);
quantity.appendChild(div);
var input2 = document.createElement('input'),
div2 = document.createElement('div');
input2.type = "text";
input2.setAttribute("name", "items");
div.appendChild(input2);
item.appendChild(div);
var input3 = document.createElement('input'),
div3 = document.createElement('div');
input3.type = "text";
input3.setAttribute("name", "serno");
div.appendChild(input3);
serial.appendChild(div);
var input4 = document.createElement('input'),
div4 = document.createElement('div');
input4.type = "text";
input4.setAttribute("name", "proper");
div.appendChild(input4);
property.appendChild(div);
};
</script>
当用户单击"添加文本"按钮时,将显示一组(四个textboxes
)。我的问题是,即使用户点击并将数据输入到textbox
中,它也只会插入一组数据(这是最后一组输入)。它是因为textbox
的"名字"是一样的。如何插入这些多个数据?或者,我如何为添加的textbox
设置一个唯一的名称,以便将它们插入数据库?
您需要在名称后面附加一个[]
。这将在表单提交时向PHP传递一个数组。
input.setAttribute("name", "quant[]");
要获取PHP中的值,
$quant_values = $_GET['quant']; // array of values
$value_one = $quant_values[0];
您将需要实现一个循环来遍历这些值。