3

我有以下 HTML 表单,它是从 MySQL 数据库中的表动态生成的。

<form method="post" name="chapters" action="ChaptersUpdate.php">
<input type='text' name='id' value='{$row['id']}'>
<input type='text' name='name' value='{$row['name']}'>
<input type='password' name='password' value={$row['password']};>
<input type='submit'>
</form>

我正在寻找一种方法,以便在提交表单时,在 $_POST 中传递以下数据结构:

 [chapter] => Array
  (
    [0] => Array
        (
            [id] => corresponding chapter ID
            [name] => corresponding chapter name
            [password] => corresponding chapter password
        )

    [1] => Array
        (
            [id] => corresponding chapter ID
            [name] => corresponding chapter name
            [password] => corresponding chapter password
        )

)

我尝试了 name='chapter[][id]' / name='chapter[][name]' / name='chapter[][password]' 的各种组合,但收效甚微。数组数据结构永远不会像我想要的那样。

有任何想法吗?

4

2 回答 2

2

以下似乎对我有用:

<input type='text' name='chapters[0][id]'>
<input type='text' name='chapters[0][name]'>
<input type='password' name='chapters[0][password]'>

<input type='text' name='chapters[1][id]'>
<input type='text' name='chapters[1][name]'>
<input type='password' name='chapters[1][password]'>
于 2012-09-24T00:00:00.200 回答
0

您可以像这样简单地创建表单

<form method="post" name="chapters">
<?php 
for($i = 0; $i <3; $i++)
{
    echo "ID: <input type='text'  name='chapters[$i][id]' /> <br />";
    echo "Name: <input type='text' name='chapters[$i][name]' /> <br />";
    echo "Password: <input type='text' name='chapters[$i][password]' /> <br /> ";
    echo "<Br />";
}
?>
<input type='submit'>
</form>

示例 PHP

if(isset($_POST['chapters']))
{
    echo "<pre>";
    print_r($_POST['chapters']);
}

样本输出

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Name1
            [password] => Password1
        )

    [1] => Array
        (
            [id] => 2
            [name] => name 2
            [password] => password 2
        )

    [2] => Array
        (
            [id] => 2
            [name] => name 3
            [password] => Password
        )

)
于 2012-09-23T23:59:48.267 回答