0

I have a similar form as here.

It has a dynamic number of inputs, e.g.

<li><input type="text" name="friend_1" id="friend_1" /></li>
<li><input type="text" name="friend_2" id="friend_2" /></li>
<li><input type="text" name="friend_3" id="friend_3" /></li>
<li><input type="text" name="friend_4" id="friend_4" /></li>
<li><input type="text" name="friend_5" id="friend_5" /></li>
</ol>
<input type="button" value="Add Friend Field" onclick="addField('friends_area','friend_',10);" />

Usually, when you want to process a form, you will refer to certain form variable as $_POST['friend'].

  1. Is there a way to collect all $_POST['friend_X'] into an array? (note that there may be gaps)
  2. and insert it into an MySQL table? Is it possible with one insert statement (I'm using PDO) or should I do it with for loop?
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
xralf
  • 3,312
  • 45
  • 129
  • 200
  • You can iterate over `$_POST` :-) the rest depends on whether you insert a single or multiple records – sodawillow Jan 06 '15 at 22:42
  • possible duplicate of [How to get form input array into PHP array](http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array) – Christian Jan 07 '15 at 07:42

2 Answers2

3

You will want to post as an array such as

<li><input type="text" name="friend[]" id="friend_1" /></li>
<li><input type="text" name="friend[]" id="friend_2" /></li>
<li><input type="text" name="friend[]" id="friend_3" /></li>
<li><input type="text" name="friend[]" id="friend_4" /></li>
<li><input type="text" name="friend[]" id="friend_5" /></li>

Then serialize the array using serialize() to save to your database.

Mike
  • 1,436
  • 9
  • 16
  • 1
    Beat me to it :-) But please don't store serialized data like that, it wil make retrieving data and searching the database a nightmare. – jeroen Jan 06 '15 at 22:50
  • Under most circumstances you are absolutely right, however to avoid the next question being "How come my insert breaks when i try to add post to db" this would be the simplest answer. – Mike Jan 06 '15 at 22:53
  • Ah, you've been here before ;-) – jeroen Jan 06 '15 at 22:55
  • When I have the label `for` attribute associated with the input `name` attribute, how could I do it now? Associate it with the input `id` attribute? – xralf Jan 06 '15 at 23:27
  • @xralf How have you associated the label with the `name` attribute? Normally it is automatically associated with the ID. – jeroen Jan 07 '15 at 15:59
2

Yes, if you use an array in html to begin with, all your values will be in an array as well:

<li><input type="text" name="friend[1]" /></li>
<li><input type="text" name="friend[2]" /></li>
<li><input type="text" name="friend[3]" /></li>
<li><input type="text" name="friend[4]" /></li>
<li><input type="text" name="friend[8]" /></li>
<li><input type="text" name="friend[9]" /></li>

In php $_POST['friend'] will be an array with the indices you set (if you set them). If you don't set the indices, they will simply start at 0.

jeroen
  • 91,079
  • 21
  • 114
  • 132