user39980
user39980

Reputation:

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?

Upvotes: 0

Views: 266

Answers (4)

user39980
user39980

Reputation:

I was thinking, I can type in:

Home, Products, About, Contact

Into a input field, then my php can break it apart using the comma and insert each of those page titles in my PAGES table.

Run the SQL within the foreach.

Thoughts?

Upvotes: 0

okoman
okoman

Reputation: 5657

You can do the following:

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

Everytime you need a new input box you can do

$(some_element).append( '<input type="text" name="list[]" />' );

or sth similar with jQuery.

Then you have an array instead of a single value in your PHP $_GET or $_POST variable. So you can use $_POST['list'] in foreach.

Upvotes: 0

David Grant
David Grant

Reputation: 14223

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
}

Upvotes: 2

Natrium
Natrium

Reputation: 31174

explode and foreach sound good.

But as Click Upvote indicated: is it for pagination? 'cause then there could be a betetr way to achieve this.

Upvotes: 0

Related Questions