如何传递提交php的动态表单的字段名称和值 [英] How to pass field name and value of dynamic form on submit php

查看:143
本文介绍了如何传递提交php的动态表单的字段名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单具有固定的字段以及从数据库创建的动态字段。这些字段可能每次都有所不同,它们是带有颜色大小等选项的下拉列表。我可以这样命名下拉列表:

  name =options []

然后执行以下操作:

  foreach($ options as $ option){
//添加到db
}

然而,只给我一些值,我需要知道optionID以及 b
$ b

可以这样做:

  name =<?echo $ optionID;?> 

的下拉菜单,但是当我尝试处理时,我不知道该表单中包含了什么optionID提交的数据。在表单中增加字段时,如何同时传递optionID和选择的值?

解决方案

可以使数组多维。这样的事情可能适用于你:

  name =options [<?php echo $ optionID;?>] [ ]

foreach($ _POST ['options'] as $ ID){
foreach($ ID as $ value){
// stuff
}
}


I have a form that has fixed fields as well as dynamic fields created from a database. the fields could be different every time and they are dropdowns with options like color size etc.I could name the dropdowns like this:

name="options[]" 

and then do something like:

foreach($options as $option) {
//add to db
}

however that only gives me the values and I need to know the optionID as well

I could do somethign like:

name="<? echo $optionID; ?>" 

for the dropdown but I don't know what optionID's were included in the form when I try to process the submitted data. how do I pass both the optionID and it's selected value when adding fields to a form dynammically?

解决方案

You can make the array multidimensional. Something like this might work for you:

name="options[<?php echo $optionID;?>][]" 

foreach ($_POST['options'] as $ID) {
  foreach ($ID as $value) {
   // stuff
  }
}

这篇关于如何传递提交php的动态表单的字段名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆