dou448172583 2012-08-22 02:12
浏览 27
已采纳

如何在php中获取动态生成的输入字段的值?

I'm working on a form where I have 2 fields which can be added N number of times. I have 2 fields called "fieldA" and "fieldB" and a button called "addrow" . So as many times I click on addrow I add fieldA and fieldB beneath previous fieldA and fieldB. So I can have unlimited fieldAs and unlimited fieldBs. I use jquery to add rows and append new fields with number behind each field's name attr to make it unique. So first set of fields will be named as fieldA1, fieldB1 and second set will be named as fieldA2 , fieldB2 and so on.

This is how my part of my form looks

enter image description here

And here's my jquery

$(document).ready(function(){
 var $i = 1;
    $('body').on('click','#addrow',function(){
    $('#fieldset').append(
    "Your Friends Name: <br /><input type='text' name='fieldA"+$i+"' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB"+$i+"' /><br /><br />"
    );
    $i++;
    });
});

Now since these fields are generated automatically using jquery I'm not sure how to fetch field's value in php.

also after fetching field's values I would also like to create an array.

This is how I expect to save values of all my fields in php array.

$fieldset = array( 
array("fieldA" => "value of fieldA1","fieldB" => "value of fieldB1"),
array("fieldA" => "value of fieldA2","fieldB" => "value of fieldB2"),
array("fieldA" => "value of fieldA3","fieldB" => "value of fieldB3"),
...
);
  • 写回答

2条回答 默认 最新

  • duanpuchun5275 2012-08-22 02:19
    关注

    You inputs must be array [], otherwise you will not be able to receive them as array fieldA[]

    Your jQuery should be like this:

    $(document).ready(function(){
     var $i = 1;
        $('body').on('click','#addrow',function(){
        $('#fieldset').append(
        "Your Friends Name: <br /><input type='text' name='fieldA["+$i+"]' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB["+$i+"]' /><br /><br />"
        );
        $i++;
        });
    });
    

    HTML

    Your Friends Name: <br /><input type='text' name='fieldA[0]'  class='name'/><br /><br />
    Your Friends Email: <br /><input type='text' name='fieldB[0]' class='email'/><br /><br />
    

    and in your PHP you will receive them as

    [fieldA] => Array (
                    [0] => 
                    [1] => 
                    [2] => 
                )
    [fieldB] => Array ( 
                    [0] => 
                    [1] => 
                    [2] =>
                )
    

    you can convert them as you want

    $newArray = array();
    
    for($i=0; $i<count($_POST['fieldA']); $i++){
        $newArray[$i]['fieldA'] = $_POST['fieldA'][$i];
        $newArray[$i]['fieldB'] = $_POST['fieldB'][$i];
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 流量太费!寻找便宜的app音视频SDK或平替方案。
  • ¥15 kubeasz部署遇到问题
  • ¥15 GUIDE to App Designer Migration Tool for MATLAB
  • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
  • ¥20 plant simulation与python com接口实时数据交互
  • ¥15 有关汽车的MC9S12XS128单片机实验
  • ¥15 求c语言动态链表相关课程有偿,或能将这块知识点讲明白
  • ¥15 FLKT界面刷新异常
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥50 单细胞测序拟时序分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部