2

I have this code: http://jsfiddle.net/Zx8hc/9/.

It's a form where a user can add input-text dynamically, he can add and delete as many as he wants (only and the first being mandatory). And since he can add and delete the ones he likes their ID's may not be consecutive.

My question is how can I collect the data from the newly created inputs with PHP and store it in vars, each input in it's own var, independently ([input_1] > $input_1, so on). My idea was to create a for loop and go through all the elements from 0 to n, and those who contain data would be stored and those who don't wouldn't. Would that be an appropriate approach?

I am sorry that I don't have any PHP code to show but I don't even know where to start with this one, so thank you very much in advance if you can help me with this one.

abraham
  • 46,583
  • 10
  • 100
  • 152
user3529213
  • 73
  • 1
  • 8

2 Answers2

1

I suggest to create these new inputs with name tags. These name tags must be unique e.g. cool_input1, cool_input_2, ... OR use as array: cool_input[].
As result - you can get incoming info in php and parse received data from POST/GET.

For the first idea you don't need to know real count of the generated inputs. You just can use 'foreach element in POST' and if its name matches your pattern - this is what you need.

Andron
  • 6,413
  • 4
  • 43
  • 56
  • OK, I will try that as soon as I get home. Thanks. – user3529213 Apr 27 '14 at 08:39
  • You are welcome. I still suggest to use an `array name` - because you don't need to do extra additional steps on js nor on php side. – Andron Apr 27 '14 at 08:41
  • An example of how to use HTML input arrays is [here](http://stackoverflow.com/a/1010970/284602). – Andron Apr 27 '14 at 08:55
  • Thanks for the additional info you saved my life. +1 By the way, if one of the arrays is a – user3529213 Apr 27 '14 at 10:11
  • Yes. It is possible to use the same name for all form elements. E.g.: [example on JSFiddle](http://jsfiddle.net/hx65y/) (just check html code). And for `select` it will work as expected (will be submitted a selected option's value). – Andron Apr 28 '14 at 12:21
1

I have checked your fiddle, and you have the next HTML as input element.

<input type="text">

If you want to send form data to a server, you have to wrap it in a form element. Here below is an example of a simple form

<form action="url_to_php_file.php" method="post">
    <input type="text" name="age[]" />
    <input type="submit" value="submit" />
</form>

Here, you see a <form> element, which you can use to pass form data to the server. It has several attributes. Here, action is the URL to where the form content should be sent to. Pick it the PHP file where the form should be handled. If it's on the same page as the display, just use # as field.

Then method-attribute is to send the form by POST data. The other option is using GET, but it's not secure because using GET will send in the form data in the URL too. While POST will wrap it in the request.

If you have a form element, it's necessary to have a button to submit the form. By submitting, you're activating the trigger to send the form to the address described in action-attribute of the form. That's the input-submit element.

Now, the data itself. Each input has to be assigned with a name-attribute. The content of it will be associated to that name when submitting the form. If you want to send in multiple data as one name field, you have to use an array, the [] in the form name.

For example, age will only hold one data-entry. While age[] can hold multiple values. If you want to add the element, just clone the said object, only if it doesn't have id with it. If you have multiple elements with same id, you can get unpredictable results. It's advisable to keep id's unique.

And on your PHP file, read the $_POST['name'] as an array.

...... edited.

user3529213
  • 73
  • 1
  • 8
KarelG
  • 5,176
  • 4
  • 33
  • 49