dtxq82489 2016-07-11 07:53
浏览 21
已采纳

如何使用PHP处理包含动态多选字段的HTML表单?

I'm chasing a bit of a challenge handling an HTML form whose input fields are created dynamically using JavaScript DOM containing multiple select fields as well. Here is a sample code of the form I have:

<input type="text" name="attributeIndex[]" id="brand" class="form-control" placeholder="brand">
<select name="attributeIndex[]" id="color" class="form-control">
<option value="92">Aero</option>
<option value="277">Aluminum</option>
<option value="91">Amber</option>
</select>

After submitting the form, I want to get the form data in a PHP array like this:

$formData=$_GET['attributeIndex'];

The array $formData should be like this:

array(
[0]=>text input value here
[1]=>array(list of selected colors here)
)

Any suggestion will be highly appreciated!

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-07-11 09:05
    关注

    Name the select with an additional []:

    <select name="attributeIndex[][]"
    

    That way each value PHP puts into $_GET will be in an array in an array.

    You'll also need to add the multiple attribute if you want more than one option to be selected.

    <select name="attributeIndex[][]" multiple id="color" class="form-control">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流