1
  • if you have many input fields and you want to insert them at once you do:

    <input type="email" name="data[email]">

then you insert $_POST['data']

  • If you want to add many input fields having the same name, using jquery or any Js library, you do this

    <input type="email" name="email[]"

Then you loop through it and insert values into Mysql.


My question is: I have both scenarios; I want to insert using

$_POST['data'] because I have many input fields, and I have a page where the user can add multiple input fields. However the following doesn't work

`<input type="email" name="data[email[]]">`

$_POST only has 1 row, the last one, so if the user generates 4 rows, only the last row's input fields values will be in $_POST.

W3 validator says that the whole page is valid, no HTML problems whatsoever.

Basically I'm appending a row of 4 input fields each time the user press add like so

http://jsfiddle.net/jaredwilli/tZPg4/4/

And var_dump($_POST[data]) is only catching the last row. $_POST[data[]) is not valid. If I try to loop through the values I get Warning: Illegal string offset because there's nothing.

Is data[example[]] valid? Or do I have to insert each field individually, not as array?

2
  • can you show a dump of $_POST? Commented Jan 7, 2018 at 10:56
  • @Akintunde007 sure, I'll notify you when on pc when I edit my question Commented Jan 7, 2018 at 10:58

1 Answer 1

2
<input type="email" name="data[email[]]">

You simply got the "syntax" wrong here, this is what you want to name the fields:

name="data[email][]"

This will create an entry in $_POST['data']['email'], that is itself a numerically indexed array holding the individual values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will give it a try tonight. Can I insert $_POST[data] directly without specifying anything?

Your Answer

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.