Skip to content
Advertisement

How do I send all the rows in the table?

You are going to send the full line value using form tag. However, if you send a value, only the value in the last row is sent. How should it be modified?

in this table code

 echo "<form action='../verification/medical_bills_check.php' method='post'>";
    while($row = mysqli_fetch_array($result)){
      
 
        echo "<tr>"; 
        echo "<td> <input type='hidden' name='id' value ='" . $row['id'] . " '>" . $row['id'] . "</td>";   
        echo "<td> <input type='hidden' name='storename' value ='" . $row['storename'] . " '>" . $row['storename'] . "</td>";    
        echo "<td> <input type='hidden' name='corporate_num' value ='" . $row['corporate_num'] . " '>" . $row['corporate_num'] . "</td>";  
        echo "<td> <input type='hidden' name='store_mbrno' value ='" . $row['store_mbrno'] . " '>" . $row['store_mbrno'] . "</td>"; 
        echo "<td> <input type='hidden' name='store_mbrno_van' value ='" . $row['store_mbrno_van'] . " '>" . $row['store_mbrno_van'] . "</td>"; 
        echo "<td ><input type='hidden' name='past_cash_receipts' value ='" . $row['past_cash_receipts'] . " '>" . $row['past_cash_receipts'] . "</td>"; 
        echo "<td> <input type='hidden' name='past_card' value ='" . $row['past_card'] . " '>" . $row['past_card'] . "</td>";                                        
        echo "<td> <input type='hidden' name='pg_select' value ='" . $row['pg_select'] . " '>" . $row['pg_select'] . "</td>";
        echo "<td> <input type='hidden' name='program_select' value ='" . $row['program_select'] . " '>" . $row['program_select'] . "</td>";
        echo "<td> <input type='hidden' name='authority' value ='" . $row['authority'] . " '>" . $row['authority'] . "</td>";   
        echo "<td> <input type='hidden' name='apikey' value ='" . $row['apikey'] . " '>" . $row['apikey'] . "</td>";   
    
        echo "<td > <input type='submit'> </td>";
        echo "</tr>";
      
    }
 echo "</form>";

and receive page code

   $_SESSION["corporate_num"] = $_POST["corporate_num"];
    $_SESSION["store_mbrno"]  = $_POST["store_mbrno"];
    $_SESSION["store_mbrno_van"] = $_POST["store_mbrno_van"];
    $_SESSION["program_select"] = $_POST["program_select"];
    $_SESSION["authority"] = $_POST["authority"];
    $_SESSION["apikey"] =$_POST["apikey"];

I want result

id        |    corporate_num   |   store_mbrno |          submit
1                  1                    1               submit_button
2                  2                    2               submit_button
3                  3                    3               submit_button
4                  4                    4               submit_button

When I submit id 1 rows. Received Page receive value id:1 / corporate_num:1/ store_mbrno:1

When I submit id 2 rows. Received Page receive value id:2 / corporate_num:2/ store_mbrno:2

Advertisement

Answer

If I understood your question correctly, the issue is that you have a lot of input fields with the same name within one form. What happens when you click one of the buttons, is that all the data in the whole form gets sent. And, because every row has fields with the same name, the values get overwritten by the next row. Therefore only the data of the last row is sent.

If you only want to send the data of one row, you need to have the data in a separate form. So, one form for each row. Since, technically, you can’t have a form element directly inside a table or tr element, I suggest putting the form element and all hidden inputs in the last table cell.

Furthermore, since the contents of the hidden inputs can be modified (you can’t trust your visitors not to use the inspector), you should not trust the information that is sent. Instead, if possible, only send the id, and fetch the other information anew from the database.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement