Multiple inputs with same name through POST in php

后端 未结 5 2121
误落风尘
误落风尘 2020-11-22 02:54

Is it possible to get multiple inputs of the same name to post and then access them from PHP? The idea is this: I have a form that allows the entry of an indefinite number

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:05

    Eric answer is correct, but the problem is the fields are not grouped. Imagine you have multiple streets and cities which belong together:

    First Address

    Second Address

    The outcome would be

    $POST = [ 'street' => [ 'Hauptstr', 'Wallstreet'], 
              'city' => [ 'Berlin' , 'New York'] ];
    

    To group them by address, I would rather recommend to use what Eric also mentioned in the comment section:

    First Address

    Second Address

    The outcome would be

    $POST = [ 'address' => [ 
                     1 => ['street' => 'Hauptstr', 'city' => 'Berlin'],
                     2 => ['street' => 'Wallstreet', 'city' => 'New York'],
                  ]
            ]
    

提交回复
热议问题