dcparham
dcparham

Reputation: 291

PHP post always ignores the last table value

I have a delete button name="action" that correctly goes to a switch/case for value="Delete", and it retrieves proper table values $_POST['fundid'] for top table value[record #1] but ignores it for last table value[record #2]. Table code:

while ($row = mssql_fetch_array($fund_info_result)) {
    echo "<tr>";
    echo "<form action='' method='post'>";
    echo "<td>" . $row['coafund'] . "</td>";

    echo "<td>" . $row['fundid'] . "</td>";
    echo "<input type='hidden' id='fundid' name='fundid' value=" . $row['fundid'] . " />";

    echo "<td>" . $row['shortdesc'] . "</td>";
    echo "<td>" . $row['longdesc'] . "</td>";

    echo "<td><input type='submit' id='delete' name='action' value='Delete' /></td></td>";
    echo "</form>";
    echo "</tr>";
}

PHP code grabs value of first record, but ignores second[last] record:

if (isset($_POST)) {
case 'Delete':
    $fundid_record = $_POST['fundid'];
    ?>
        <script type="text/javascript">
            window.alert("<?php echo $fundid_record;?>");
        </script>
    <?php
break;
}

A sample of the generated HTML looks like this:

<tbody>
  <form id='table_data1' action='' method='post'>
  <tr>
    <td>139</td>
    <td>14966</td>
    <input type='hidden' id='14966' name='fundid' value=14966 />
    <td>Admin</td>
    <td>Administration</td>
    <td><input type='submit' id='delete' name='action' value='Delete' /></td>
    </td>
  </tr>
  </form>
  <form id='table_data1' action='' method='post'>
    <tr>
      <td>140</td>
      <td>14967</td>
      <input type='hidden' id='14967' name='fundid' value=14967 />
      <td>Services</td>
      <td>Services</td>
      <td><input type='submit' id='delete' name='action' value='Delete' /></td>
      </td>
    </tr>
  </form>
</tbody>

Another user ticket here helped me at least grab the value properly, using form tag for each record, and hidden value, but very confused why it ignores last record of the table. Please help.

Upvotes: 0

Views: 73

Answers (2)

ADyson
ADyson

Reputation: 61915

You are creating invalid HTML markup. The only thing that can be a child of a <tbody> is a <tr>, and the only things that can be a child of a <tr> is a <th> or <td>. Putting your form and input code within the table but outside a table cell is invalid. Many browsers will shift markup like this outside your table, which will cause the form to be incorrectly created, and is almost certainly the cause of the strange behaviour you're seeing.

Also you close one of the <td>s twice (</td></td>) which isn't helping either.

This should fix it:

echo "<tr>";
echo "<td>" . $row['coafund'] . "</td>";
echo "<td>" . $row['fundid'] . "</td>";
echo "<td>" . $row['shortdesc'] . "</td>";
echo "<td>" . $row['longdesc'] . "</td>";
echo "<td><form action='' method='post'><input type='hidden' name='fundid' value=" . $row['fundid'] . " /><input type='submit' id='delete' name='action' value='Delete' /></form></td>";
echo "</tr>";

Upvotes: 2

Sarkouille
Sarkouille

Reputation: 1232

A var_dump() of your $_POST could offer useful information.

From what I see, you are using static values in id attributes for elements inserted in a loop. That's a bad idea whatever happens next, since an id is supposed to exist once and only once in a single page. You should at least increment a value each time the loop iterates and append that value to your id so that it's unic, or not use an id at all.

I don't have enough information to be sure about it, but I in fact encountered that same problem of yours not so long ago, and the cause was precisely that one id was used several times in different form elements.

Hope it helps and feel free to add some logs if it doesn't.

Upvotes: 1

Related Questions