Page 1 of 1

Process form with dynamicly generated field names

Posted: Sun Jun 05, 2011 2:37 pm
by edpatterson
[Extreme newbie alert!]
I am trying to process a form that uses dynamically generated field names.

This is for my own use so it doesn't need to be pretty, just accurate :-) I currently using a spreadsheet to track a list of available items, which ones are in use and how many are in use on a particular day.

Not that it should matter but it is running: Linux, MySQL, PHP5, Apache2

items table
itemID int
itemName varchar
itemInUse tinyInt (boolean)
PRIMARY KEY(itemID)

inventory table
date_stamp
itemID
itemCount
PRIMARY KEY(date_stamp, itemID)

query to gather data for table
select itemID,itemName from items where itemInUse = True

what I am using to create the table

Code: Select all

  print("<table border='1'>\n");
  print("<tr><td>Item ID</td><td>Item Name</td><td>Count</td></tr>\n");
  while($row = mysql_fetch_row($result)){
    print("<tr><td>".$row[0]."</td><td>".$row[1]."</td><td><input type='text' name='item-".$row[0]."'</td></tr>\n");
  }
...
Then above is working fine, it the processing part that is confusing me. I have unique text field names like item-1, item-6, item-54. The item-xx will not be in any particular order nor will every item have a value. item-1 may have the value of 22 and item-2 may be null or zero.

I can't find any documentation for stepping through the POST data for unknown 'names'.

I wish I could describe this better.

Thanks,
Ed

Re: Process form with dynamicly generated field names

Posted: Sun Jun 05, 2011 3:41 pm
by oscardog
Well, I only skimmed over it but the easist way would be to use empty() to check if the $_POST variable has been set. Just use a foreach() loop on the $_POST array or something.

Re: Process form with dynamicly generated field names

Posted: Sun Jun 05, 2011 10:41 pm
by flying_circus
I was actually thinking about this earlier today, but for post files. Anyways, here is an example for you:

Code: Select all

<?php
  print_r($_POST);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>Untitled</title>
  </head>
  <body>
    <form method="post">
      <input type="text" name="dynField[1]" value="?" />
      <input type="text" name="dynField[3]" value="?" />
      <input type="text" name="dynField[7]" value="?" />
      <input type="text" name="dynField[13]" value="?" />
      <input type="text" name="dynField[9]" value="?" />
      <input type="text" name="dynField[16]" value="?" />
      <input type="text" name="dynField[1000]" value="?" />
      <input type="submit">
    </form>
  </body>
</html>
and the relevant PHP info ends up as:

Code: Select all

Array
(
    [dynField] => Array
        (
            [1] => ?
            [3] => ?
            [7] => ?
            [13] => ?
            [9] => ?
            [16] => ?
            [1000] => ?
        )
 
)
Now that you have an array, you can use a foreach statement to pull out each value:

Code: Select all

<?php
  foreach($_POST['dynField'] AS $name => $value) {
    print "Key Name: {$name}, Value: {$value}";
  }
?>