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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测