Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

is it possible to have double nested input tag arrays in html?

I'm wondering before I attempt to refactor my page if its possible to have a double nested input array in html. I have an 8X5 group of elements in a form and it would be nice for me to be able to parse it using an array of arrays...something like

    <input type="text" name="list[][]" /><input type="checkbox" name="list[][]" /> 

and so on

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are going to need to supply indexes into the first part of each field or else there is nothing to nest, and if it did work, it wouldn't look like a grid on the other end:

Row 1:

 name="list[0][]"

Row 2:

 name="list[1][]" 

etc.

Finally, your server needs to support this as PHP and Rails do out of the box. I am not sure about other server technologies. For an example, the following HTML when posted to PHP:

<form action="post.php" method="POST" accept-charset="utf-8">
  <input type="text" name="list[0][]" value="1" />
  <input type="text" name="list[0][]" value="2" />
  <input type="text" name="list[0][]" value="3" />

  <input type="text" name="list[1][]" value="4" />
  <input type="text" name="list[1][]" value="5" />
  <input type="text" name="list[1][]" value="6" />

  <input type="text" name="list[3][]" value="7" />
  <input type="text" name="list[3][]" value="8" />
  <input type="text" name="list[3][]" value="9" />

  <input type="submit" name="Send" value="Send" id="Send" />
</form>

If in the PHP the following code exists:

<?php print_r($_POST['list']); ?>

The output is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [3] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...