我有一个公司模型的表格。公司在我的模型中有一个或多个联系人,所以在我的表单中我有client_name,client_phone,client_email的输入,我可以用我做的一点点javascript重复它们,这样我就可以添加任意数量的客户端。我需要发布这个并将这些对象分组以便能够预测它们,我的意思是这样的:
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}
]
如何命名输入以获取此信息?
答案 0 :(得分:2)
如果我正确地阅读你的问题,你可以这样做:
<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]
);
}