0

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.

2
  • 1
    Write print_r($_POST); in your code to see what you have to work with. Commented Apr 23, 2012 at 11:32
  • Webbandit: Excuse me, what? I don't understand what you mean. Commented Apr 23, 2012 at 11:45

4 Answers 4

1

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.

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

Comments

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

2 Comments

i want to access the ItemName[1] separately and ItemName[2] separately?
If you want to access $ItemName[1] or $ItemName[2] directly, instead of $_POST['ItemName'][1] or $_POST['ItemName'][2], you can use extract($_POST), and this way you will be able to access all the $_POST variables directly (i.e. $ItemName[1] instead of $_POST['ItemName'][1]). :)
0

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

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

Comments

0

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.

Comments

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.