[英]Get values of all selected items from user-created select fields
I have a select field with a add button next to it inside a form. 我在表单中有一个选择字段,旁边有一个添加按钮。 The user will click the add button and create as many extra select fields as he wants and then select an item from each of the select fields. 用户将单击添加按钮,并根据需要创建尽可能多的额外选择字段,然后从每个选择字段中选择一个项目。 When he submits the form, I want the values of all selected items in an array. 当他提交表单时,我想要数组中所有选定项的值。
Can any one help please? 有人可以帮忙吗? Thanks. 谢谢。
<?php
session_start();
include ('../conn.inc.php');
?>
<!DOCTYPE>
<html>
<head>
<script>
function addRow(r){
var root = r.parentNode;
var allRows = root.getElementsByTagName('tr');
var cRow = allRows[0].cloneNode(true)
var cInp = cRow.getElementsByTagName('input');
for(var i=0;i<cInp.length;i++){
cInp[i].setAttribute('name',cInp[0].getAttribute('name')+'_'+(allRows.length+1))
}
var cSel = cRow.getElementsByTagName('select')[0];
cSel.setAttribute('name',cSel.getAttribute('name')+'_'+(allRows.length+1));
root.appendChild(cRow);
}
</script>
</head>
<body>
<form method="get" action="abc()">
<table>
<tr>
<td>
<select name="product_code" id="product_code">
<?php
$sql= mysql_query("select * from product_table where id > 0 order by product_code");
while($row = mysql_fetch_array($sql))
{
$pcode = $row['product_code'];
echo "<option value=\"$row[id]\">".$pcode."</option>";
}
?>
</select>
</td>
<td>
<input name="button" type="button" value="+" onick="addRow(this.parentNode.parentNode)">
</td>
</tr>
</table>
<input type="submit" value="Submit" />
</form>
</body>
</html>
It's more simple than what you've done. 这比您所做的要简单。
You should set the name
attribute of all your selects something like productCode[]
(with the brackets). 您应该设置所有选择的name
属性,例如productCode[]
(带有方括号)。
PHP will automatically create you an $_POST['productCode']
variable containing all the values in an array.. PHP将自动为您创建一个$_POST['productCode']
变量,该变量包含数组中的所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.