PHP Post 组重复输入
PHP Post group of repeated inputs
我有一个公司模型的表格。一家公司在我的模型中有一个或多个联系人,所以在我的表格中我有 client_name、client_phone、client_email 的输入,我可以用一点 javascript 重复它们所以我可以添加任意数量的客户。我需要 post 这个并将这些对象分组以便能够 foreach 'em,我的意思是这样的:
client: [
{name: "Alan", phone: "123456123", email: "alan@example.com},
{name: "Alan", phone: "123456123", email: "alan@example.com},
{name: "Alan", phone: "123456123", email: "alan@example.com}
]
应该如何命名输入才能得到这个?
如果我没看错你的问题,你可以这样做:
<form method="post">
<input name="client[0][name]" value="Alan">
<input name="client[0][phone]" value="123456123">
<input name="client[0][email]" value="alan@example.com">
<input name="client[1][name]" value="Alan">
<input name="client[1][phone]" value="123456123">
<input name="client[1][email]" value="alan@example.com">
<input name="client[2][name]" value="Alan">
<input name="client[2][phone]" value="123456123">
<input name="client[2][email]" value="alan@example.com">
<input type="submit">
</form>
这将给出一个这种格式的数组,准备好 json_encode
ing:
Array
(
[client] => Array
(
[0] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
[1] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
[2] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
)
)
另一个选项参考评论:
<form method="post">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input type="submit">
</form>
和 PHP 重建数组:
$array = array();
foreach ($_POST['name'] as $key => $name) {
$array[] = array(
'name' => $name,
'phone' => $_POST['phone'][$key],
'email' => $_POST['email'][$key]
);
}
我有一个公司模型的表格。一家公司在我的模型中有一个或多个联系人,所以在我的表格中我有 client_name、client_phone、client_email 的输入,我可以用一点 javascript 重复它们所以我可以添加任意数量的客户。我需要 post 这个并将这些对象分组以便能够 foreach 'em,我的意思是这样的:
client: [
{name: "Alan", phone: "123456123", email: "alan@example.com},
{name: "Alan", phone: "123456123", email: "alan@example.com},
{name: "Alan", phone: "123456123", email: "alan@example.com}
]
应该如何命名输入才能得到这个?
如果我没看错你的问题,你可以这样做:
<form method="post">
<input name="client[0][name]" value="Alan">
<input name="client[0][phone]" value="123456123">
<input name="client[0][email]" value="alan@example.com">
<input name="client[1][name]" value="Alan">
<input name="client[1][phone]" value="123456123">
<input name="client[1][email]" value="alan@example.com">
<input name="client[2][name]" value="Alan">
<input name="client[2][phone]" value="123456123">
<input name="client[2][email]" value="alan@example.com">
<input type="submit">
</form>
这将给出一个这种格式的数组,准备好 json_encode
ing:
Array
(
[client] => Array
(
[0] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
[1] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
[2] => Array
(
[name] => Alan
[phone] => 123456123
[email] => alan@example.com
)
)
)
另一个选项参考评论:
<form method="post">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input name="name[]" value="Alan">
<input name="phone[]" value="123456123">
<input name="email[]" value="alan@example.com">
<input type="submit">
</form>
和 PHP 重建数组:
$array = array();
foreach ($_POST['name'] as $key => $name) {
$array[] = array(
'name' => $name,
'phone' => $_POST['phone'][$key],
'email' => $_POST['email'][$key]
);
}