0

我有一个业主协会的登记表。房主实际上可以拥有多个需要注册的地块。我通过允许用户单击将文本字段添加到表单以允许附加数据的按钮来做到这一点。可以注册的额外批次数量没有限制。我的问题是如何管理帖子名称?地址、城市、州和邮政编码都需要组合在一起。基本上,我怎样才能得到这个结果:

$_POST = array(
    'mainAddress' => array(
        'address' => '',
        'city' => '',
        'state' => '',
        'zipcode' => ''
    )
    'additionalAddresses' => array(
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        ),
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        ),
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        )
    )
);

这甚至可能吗?如果没有,我能做些什么来接受额外的地址?

4

1 回答 1

4

HTML:

    <form>
Address 1
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">

Address 2
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">

Address 3
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">
    </form>

PHP代码:

$new_address_array = array();
foreach($_POST['address'] as $k=>$v){
$temparray['address'] = $v;
$temparray['city'] = $_POST['city'][$k]; 
$temparray['state'] = $_POST['state'][$k]; 
$temparray['zip'] = $_POST['zip'][$k]; 
$new_address_array[] = $temparray;
}

$final['addtional_address']=$new_address_array;

print_r($final);
于 2012-06-13T05:03:41.793 回答