variableUnknown
variableUnknown

Reputation: 33

Post an "array" on submit form

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!

Upvotes: 1

Views: 1054

Answers (2)

Quentin
Quentin

Reputation: 944439

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

Upvotes: 0

Barmar
Barmar

Reputation: 782498

Use [] in the input names:

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

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

Upvotes: 4

Related Questions