Well in my previous post, you were able to add multiple dynamic fields using jQuery. In this tutorial, we will collect values from those dynamically generated input fields, which can be displayed on user browser or store in MySql database.
Let’s assume you have a HTML form with multiple input fields like example shown below. This fields are generated by jQuery code in my previous post.
Notice the name attribute mytext in input fields? they all have same name. The brackets indicate that the value of such input field will be posted as an array, so another input field means just another value in existing array.
Collecting values from above input fields is pretty simple, take a look at example below. Once the value is captured from input fields, you can output it on the browser.
We can directly access array values using index number, but this will cause error if indexed value doesn’t exist in array.
Another way is using PHP implode(), it converts array into string, separated by commas or any character. Just make sure POST variable is not empty:
You can also iterate POST variable using foreach like so, result is same as above.
How about converting the values into JSON string, just use PHP json_encode(), you can easily decode it back to array using json_decode():
Save strings on your database using MySqli.