简体   繁体   English

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

[英]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.

相关问题 如何从jsp中的选择框(选中和未选中)获取所有值 - How to get all values from a select box(selected & not selected) in jsp 如何在特定主题标签中获取 Instagram 用户创建的帖子? - How to get Instagram user-created posts within a particular hashtag? 如何从中获取所有选定的值<select multiple=multiple>? - How to get all selected values from <select multiple=multiple>? 在元素之间翻转文本输入字段,包括用户创建的value属性 - Flip text input fields between elements, including user-created value attribute 自定义用户创建的Google地图? - Customizing a user-created google map? 在MongoDB中存储用户创建的逻辑 - Storing user-created logic in MongoDB 从选择多个=“多个”选项下拉列表中获取所有选定项目的名称 - Get the name of all selected items from select multiple=“multiple” options dropdown 在“提交”表单上获取“多选”下拉列表中的所有选定项 - Get all selected items of a Multi Select Dropdown on form Submit 是否可以将位置从用户创建的地图导入到Google Map API? - Is it possible to import locations from user-created maps to the google map API? 部署用户事件脚本以从Netsuite ERP中部署的所有套件中获取所有字段和值 - Deploy user event script to get all fields and values from all suitelets deployed in Netsuite ERP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM