PHP creating an array from form input fields

Lonergan6275 picture Lonergan6275 · Sep 21, 2012 · Viewed 23k times · Source

I'm trying to create an array to store numbers and add them together.

<input type="number" name="numbers[]"/>

I'm getting an undefined variable on the following line

foreach($numbers as $number)

I'm sure this is probably something basic but I'm relatively new to php and help would be greatly appreciated.

Answer

thewebguy picture thewebguy · Sep 21, 2012

If you posted the inputs you're showing from one page to another and you need to run through the list you should set it up like this:

if (isset($_REQUEST['numbers']) && is_array($_REQUEST['numbers'])) {
  $numbers = $_REQUEST['numbers'];

  foreach ($numbers as $number) {
    print $number;
  }
}