-1

This foreach generates a list of choices. When selecting any choice from the list it only passes the data from the last available choice, not the one the user selects. I know it isn't clean or secure so no worries there. . .(one step at a time. . .) I just need it to pass the data for the item selected instead of always the last one in the list of choices.

       <?php foreach ($result as $row) : ?>
    <form action="selected-results.php" type="get"> 
    <tr>
    <td><input type="text" name="offnum" value="<?php echo escape($row["offnum"]); ?>" size="11" readonly></td>
    <td><input type="text" name="offdesc" value="<?php echo escape($row["offdesc"]); ?>" size="20" readonly></td>
        <td><input type="text" name="stafn" value="<?php echo escape($row["stafn"]); ?>" size="10" readonly></td>
        <td><input type="text" name="staln" value="<?php echo escape($row["staln"]); ?>" size="10" readonly></td>
        <td><input type="text" name="slots" value="<?php echo escape($row["slots"]); ?>" size="3" readonly></td>
        <td><input type="text" name="olitaclvl" value="<?php echo escape($row["olitaclvl"]); ?>" size="1" readonly></td>
        <td><input type="text" name="omathaclvl" value="<?php echo escape($row["omathaclvl"]); ?>" size="1" readonly></td>
    <td><input type="text" name="stuid" value="<?php echo "$stuid"; ?>" size="5" readonly></td>        
    <td><input type="text" name="stufn" value="<?php echo "$stufn"; ?>" size="10" readonly></td>
    <td><input type="text" name="stuln" value="<?php echo "$stuln"; ?>" size="10" readonly></td>
    <td><input type="submit" value="Select"></td>
    </tr>
<?php endforeach; ?>

I thought by putting each choice in a form by itself would make it pass the data from that specific form (choice) but it doesn't. It always passes the last choice in the list no matter which the user selects. The data in the choices is read from a database and displayed in the choices list. The user clicks their selection using the "Select" button at the end of each choice. Thank you for any insights. I have tried <form action="selected-results.php" type="post"> but that didn't work either.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
veewee77
  • 1
  • 3

1 Answers1

1

Since you're working with arrays, you need to set your input names with array brackets [] which will formulate your $_POST as an array:

<td><input type="text" name="results[<?php echo $row['id']; ?>][offnum]" value="<?php echo escape($row["offnum"]); ?>" size="11" readonly></td>

Ideally you should create one simple array (In the case above, you can access with with $_POST['results']). There are a few alternatives, if you don't have a $row['id'] column:

<td><input type="text" name="offnum[]" value="<?php echo escape($row["offnum"]); ?>" size="11" readonly></td>
<td><input type="text" name="offdesc[]" value="<?php echo escape($row["offdesc"]); ?>" size="20" readonly></td>

PHP code:

foreach ($_POST['offnum'] AS $i => $offnum)
{
    $offdesc = $_POST['offdesc'][$i];
    //...
}
Blue
  • 22,608
  • 7
  • 62
  • 92
  • When I changed the input lines as in the first two examples above, nothing gets passed. Just empty fields. And I don't know how to use the info in the PHP code: section. In the example it mentions a $row['id'] column. In that data, the offnum column is the id column if that is the auto-incremented id field. Frustrating! – veewee77 Nov 15 '19 at 03:44