Page 1 of 1

Dynamic Form Processing

Posted: Wed May 20, 2009 8:48 am
by hybernate20
Hello. I've been working on a project for a little while, and I've hit a bit of a wall here.

I have a page that has a form on it. Using Javascript, the user can add as many fields as they need to a certain section. The input fields are dynamically named with a counter variable, so they will be like "field0", "field1", field2" etc. depending on how many the user adds. How can I process this with PHP? What is getting me is the fact that I don't know how many fields there will be, and I don't know how to find out on the fly with PHP... any ideas?

Sorry if I'm being vague here...

Thanks,

Re: Dynamic Form Processing

Posted: Wed May 20, 2009 8:55 am
by onion2k
What you asked for:

Code: Select all

foreach ($_POST as $key => $value) {
  if (strpos($key,"field")===0) {
    $key = str_replace("field","",$key);
    echo $key."-".$value;
    echo "<br />";
  }
}
However, it'd be better to add the fields with a name of "field[]" instead, that way they'll be an array in $_POST instead. Much easier to use.

Re: Dynamic Form Processing

Posted: Wed May 20, 2009 3:16 pm
by hybernate20
Thanks! You are right, it would make much more sense to just make myself an array in POST. This was very helpful, thanks for your advice!