15

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

2
  • What is your server processing language? I believe PHP can handle this. Commented Dec 30, 2009 at 6:14
  • Darrell: im using php. What im wondering is if $_POST['elem_name'] would be an array of arrays or if it would be garbage. Commented Dec 30, 2009 at 6:19

2 Answers 2

22

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
        )

)
Sign up to request clarification or add additional context in comments.

6 Comments

So you are just giving the fields names that have a number. You are faking the functionality, it's not really supported by HTML4.
so what if i am? I'm just trying to make it a little more elegant to do my php code on the other end of the form :)
I updated my answer to provide an example in PHP. @voyager, I am not really faking anything since the fields are valid HTML4 names and its up to the server technology to support it.
@Doug Neiner: What I tried to say was that HTML doesn't have a native way of sending a multidimensional array, you have to fake it by either pickleing it into one variable or using one variable per dimension.
Is there any possibility to do same in ASP.NET MVC? or is there any other alternatives?
|
1

HTML allows you to have several inputs with the same name, which are sent to the server through POST or GET as a comma separated array, which most (all?) server side languages recognize as a native array.

There is no native way of making a multidimensional array with pure HTML without you rolling something yourself with javascript.

2 Comments

PHP does not recognize fields with the same name as an array. In fact, it will only return the last defined value. d=1&d=2&d=3 when retrieved with $_GET['d'] would return "3"
@Doug Neiner: I've never used PHP for anything bigger than a hello, world, so I assumed it worked like ASP, Python, Ruby and (I think) Perl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.