繁体   English   中英

从用户创建的选择字段中获取所有选定项的值

[英]Get values of all selected items from user-created select fields

我在表单中有一个选择字段,旁边有一个添加按钮。 用户将单击添加按钮,并根据需要创建尽可能多的额外选择字段,然后从每个选择字段中选择一个项目。 当他提交表单时,我想要数组中所有选定项的值。

有人可以帮忙吗? 谢谢。

<?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> 

这比您所做的要简单。

您应该设置所有选择name属性,例如productCode[] (带有方括号)。

PHP将自动为您创建一个$_POST['productCode']变量,该变量包含数组中的所有值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM