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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输