0

I'm creating a form from a database and the input id's could be 1-9, 1,2,5,8, etc. IE with the way it is now, I cannot determine what the number will be unless I were to iterate from number 1 to the final number of menu items in the database... which I imagine is not optimal from a coding perspective.

I have two files. File1 will get list number of menu items from a database and create a list. The condensed version of my code is as follows, please keep in mind i have condensed a lot of useless stuff;

File1.php

    $menuArray = openMenu(1);
    $return = "<div id='menu'><form method='post' action='file2.php'><input type='submit' name='submit' value='Commit Order' /><table class='tableinfo'>";
    $i=1;
    foreach($menuArray as $recordNum => $record)
    {
            if ($record['available'] > 0)
            {
                    $thisClass='available';
            } else{
                    $thisClass='unavailable';
            }

            $return.="<tr class='$thisClass'>
                            <td>$record[itemid]</td>
                            <td><label for='$record[itemid]'>$record[name]</label></td>
                            <td><button type='button' id='itemid-$record[itemid]' class='subtract'>-</button><input class='itemadder' id='itemid-$record[itemid]' type='number' min='0' value='0' /><button id='itemid-$record[itemid]' class='addition' type='button'>+</button></td>
                    </tr>";
    }
    $return.="</table></form></div>";

    return $return;

File2.php I don't know how to code this :(

Is anyone able to shed some light on the best way to do this? I just need a way to be able to see what id's have a value when posted.

I am using jQuery at the moment; would this be something best done using jquery?

The Blue Dog
  • 2,455
  • 3
  • 16
  • 25
eLusive
  • 13
  • 6
  • I have picked Overachievers answer as it was the simplest to implement. I'm still learning a fair bit - hence this little system I'm making - however I struggled trying to get Axels answer working :( Both are great answers, but I feel that Overachievers answer is the simplest for the application that I'm building and my current skill level. Thank you both for your amazing replies! I wish I could upvote each answer however I don't have enough reputatoin. – eLusive Aug 25 '14 at 12:31

2 Answers2

0

The script File1.php rendering the inputs above does know about what has rendered out. So what, if it also puts a list of rendered form element names in to the session for later use in file2.php:

In the beginning:

 $_SESSION['formids'] = array();

in the loop:

 ....
 $_SESSION['formids'][] = "itemid-" . $record[itemid];

and in file2.php:

 $sendItems = $_SESSION['formids'];
 ...

 foreach ( $sendItems as $itemId )
 {
      $val = empty($_POST[$itemId]) ? null : $_POST[$itemId];

      if ( isset($val) )
 ...
Axel Amthor
  • 10,616
  • 1
  • 20
  • 42
0

Assuming I understand you correctly the best way to do this would be to have an array of inputs.

Code you should try to achieve for your HTML output would need to be something like this:

<input type="text" name="number[1]" value="" />
<input type="text" name="number[3]" value="" />
<input type="text" name="number[5]" value="" />

Now you know after your form submission in PHP:

foreach($_POST['number'] as $id => $value){
    echo 'The value for ID #' . $id . ' is ' . $value; 
}
Overachiever
  • 825
  • 6
  • 10
  • Interestingly this array format is breaking the jQuery I have. Just a note I feel should be added. As mentioned here http://stackoverflow.com/a/13117375/1402441 it seems to be a valid element ID however it's not working. Odd. – eLusive Aug 25 '14 at 13:23
  • Your jQuery can be refactored to work pretty easily. I'm guessing on it's functionality based off what you've provided but some pseudo code would be on click of either of the buttons do $(this).closest('td').find('input') to get the textbox you are working with. – Overachiever Aug 25 '14 at 14:47