【问题标题】:How would I create this array structure in an HTML form?如何在 HTML 表单中创建这个数组结构?
【发布时间】:2012-09-15 10:55:51
【问题描述】:

我有以下 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]' 的各种组合,但收效甚微。数组数据结构永远不会像我想要的那样。

有什么想法吗?

【问题讨论】:

    标签: php mysql arrays multidimensional-array


    【解决方案1】:

    以下似乎对我有用:

    <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]'>
    

    【讨论】:

      【解决方案2】:

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

      <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
              )
      
      )
      

      【讨论】:

      • 对,所以我实际上正在做类似的事情。实际创建这些输入字段的 PHP 组件是: while ($row=mysql_fetch_array($result)) { echo ""; $result = mysql_query("SELECT * FROM chapters").奇怪的是,当我检查实际生成的 HTML 页面的源代码时,它没有显示“chapter[1][id]”......它只显示“chapter[][id]”。这可能是问题的一部分吗?
      • 看看我的代码你会看到for($i = 0; $i &lt;3; $i++) ...它们是什么时候创建的......
      • ohhhh,所以在我的情况下,for 循环是否会进入我一开始的 while() 循环中?
      猜你喜欢
      • 2017-02-03
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多