1

My code

<form action="index.php" method="post">
      <input type="text" name="item" placeholder="item"><br>
      <input type="text" name="item" placeholder="item"><br>
      <input type="text" name="item" placeholder="item"><br>
      <input type="submit" name="submit" value="submit">
  </form>

What you see here is a simplefied version of the code I have right now. Others things you can't see at the moment are: there is a function to add more rows (jQuery) and to delete rows (also jQuery). So like name='item(countednumber)'), looks to me somehting not to implement. And I also shouldn't know how I can get all the posted values of item1, item2, item3.... etc.

Question
Is there a way of like posting item in sort of array or something? Just how can I get all the values posted of item and get the posted values in a correct order/way?

I hope someone can help me with my "problem", thanks in advance!

2 Answers2

4

Use [] in the input names:

<input type="text" name="item[]" placeholder="item">

Then $_POST['item'] will be an array of all the inputs.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

PHP will create an array of data within $_GET or $_POST if the name ends in [] (or [someIndex]).

So:

  <input type="text" name="item[]" placeholder="item"><br>
  <input type="text" name="item[]" placeholder="item"><br>
  <input type="text" name="item[]" placeholder="item"><br>

and then

  $_POST["item"] # will be an array
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335