sqlchild
sqlchild

Reputation: 9064

Receive multidimensional text box array in PHP

I have text box values to be posted . How do I take it in a PHP array .The html is as follows:

<input type="text" name="ItemName[1][2]" >
<input type="text" name="ItemName[1][3]" >
<input type="text" name="ItemName[1][4]" >

$ItemNamesArray = $_POST[] ..... ????? What do I do in this step???

Please help.

Upvotes: 0

Views: 596

Answers (4)

Anthony
Anthony

Reputation: 37065

Well, in your example:

<input type="text" name="ItemName[1][2]" >
<input type="text" name="ItemName[1][3]" >
<input type="text" name="ItemName[1][4]" >

The $_POST key is going to be the name, so ItemName and because PHP treats the follow-up brackets as array keys, the values would be:

$_POST['ItemName'][1][2]
$_POST['ItemName'][1][3]
$_POST['ItemName'][1][4]

So if you want all item names, go with:

$itemNames = $_POST['ItemName'];

FYI,

  1. you should sanitize and validate all input before using it.

  2. If you don't actually have an ItemName[2][1] or ItemName[0][1] etc, then the following would make more sense:

     <input type="text" name="ItemName[]" />
     <input type="text" name="ItemName[]" />
     <input type="text" name="ItemName[]" />
    

Which would just set the index as they came in, instead of pre-setting them.

Upvotes: 0

Sethunath K M
Sethunath K M

Reputation: 4761

The post array can be accessed like $ItemNamesArray = $_POST['ItemName']

and you can access the first item $ItemNamesArray[1][2]

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

If you use var_dump($_POST), you'd see the answer right away:

array(1) {
  ["ItemName"]=>
  array(1) {
    [1]=>
    array(3) {
      [2]=>
      string(1) "a"
      [3]=>
      string(1) "b"
      [4]=>
      string(1) "c"
    }
  }
}

So you access $_POST['ItemName'][1][2] for example.

Upvotes: 2

A.N.M. Saiful Islam
A.N.M. Saiful Islam

Reputation: 2138

$_POST['ItemName'][1][2];
$_POST['ItemName'][1][3];
$_POST['ItemName'][1][4];

Upvotes: 0

Related Questions