How to send multiple related values from my html form to server via $_POST?

I was going to use jQuery to clone the input field when I click a button to make another field, but I was thinking of doing:

Page One, Page Two, Page Three <- then send that as one $_POST to the server and have it take each page and break on the “,” comma then for each insert in to my POSTS table.

Any idea on how I would do that? Would I use explode()? Then inside a foreach() run a query for each item in the field.

So if there were 5 pages typed in separated by commas, it would run five times using the foreach().

for each(){
  EXECUTE SQL HERE
}

Does that sound like it would be the best way of going about it?

Answer

If you set the name attribute of the input element to the name of an array, e.g. foo[], you’ll be able to access it as such when processing the $_POST vars, e.g.

<input name="foo[]" type="text"/>
<input name="foo[]" type="text"/>

becomes two iterations of:

foreach ($_POST['foo'] as $foo) {
    // process $foo
}

Leave a Reply

Your email address will not be published. Required fields are marked *