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!

asked Jan 9, 2017 at 22:31
3
  • you can get all the post variables using a while loop Commented Jan 9, 2017 at 22:33
  • @VermaJr. — Not with PHP with those names. Commented Jan 9, 2017 at 22:35
  • 1
    Possible duplicate of Posting array from form Commented Jan 9, 2017 at 22:35

2 Answers 2

4

Use [] in the input names:

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

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

answered Jan 9, 2017 at 22:33
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm, thanks for the quick answer! I will accept your answer in 8 minutes
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
answered Jan 9, 2017 at 22:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.