0

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?

2
  • what are you trying to do? pagination? what are the buttons for? Commented Feb 16, 2009 at 12:47
  • Not for pagination, for I can type in: "About, Contact, Products" in to an input then insert EACH of those separated by comma and place them in my PAGES table, so run three different queries. Commented Feb 16, 2009 at 14:27

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.