douben8492 2011-03-15 02:24
浏览 44
已采纳

带有PHP和嵌套数组输入的动态表单

I have a project that requires the process of order entry. An order will be as simple as choosing an item from a drop down list. Each order may contain multiple line items for the purchase. Using JQuery to clone this field is quite simple as I understand and I can get that far. Where I get hung up is there will be a collection of options relating to the item selected for the order that needs to be tied to that particular line item. A typical order may look something like:

  • PRODUCT ITEM: ASUS BASE DESKTOP
    • OPTIONS
    • 4GB RAM Option
    • Windows 7 x64 Professional
  • PRODUCT ITEM: HP BASE LAPTOP
    • OPTIONS
    • 2GB RAM Option
    • Windows Vista x32

My HTML markup looks something like:

<tr class="firstProduct productOrder">
    <td>Product Name</td>
    <td>
        <select id="ProductNameSelect" name="productOrdered[]">
            <option selected="selected" value="0">Select a Product...</option>
            <option value="1">ASUS BASE DESKTOP</option>
            <option value="2">HP BASE LAPTOP</option>
        </select>
    </td>
</tr>
<tr class="firstProduct productOrder">
    <td>Product Options</td>
    <td>
        <label>4GB RAM Option<input type="checkbox" name="productOption[0][]" value="1" /></label>
        <label>Windows 7 x64 Professional<input type="checkbox" name="productOption[0][]" value="2" /></label>
        <label>Windows Vista x32<input type="checkbox" name="productOption[0][]" value="3" /></label>
    </td>
</tr>

The name of the productOption fields have to be named to a nested array or else options selected for productOrdered[1] may not get properly assigned. I need the ability to iterate the name of the productOption from [0][ ] to [1][ ] to [2][ ] and so on for each order added so it can be processed properly in the POST variable for PHP.

Is there a "best method" or at least more common method for accomplishing this goal other than to search for the previous child in JQuery and the parse the name to determine the next ID to be used in the name for productOption?

  • 写回答

1条回答 默认 最新

  • double820122 2011-03-15 02:28
    关注

    Yes, simply declare a global variable in your javascript code and increment it everytime you add a fieldset.

    <script>
    var index=0;
    function addFieldset()
    {
       ...
       index++;
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题