0

HTML button not selecting correct form value. The following code selects data and displays it correctly in a html table: fname, lname , customer_id and the last column in the table shows a button labelled more info. The first part of the code works fine.

<table>
   <thead>
      <tr>
         <th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
      </tr>
   </thead>
   <tbody>
     <tr>
         <?php 
         $sql = "select fname, lname,customer_id from customer_address ";
         $result = mysqli_query($conn,$sql);

         while($row = mysqli_fetch_assoc($result)){
            $fname = $row['fname'];
            $lname = $row['lname'];
            $customer_id = $row['customer_id'];
            
         echo" <tr>";
          echo"<td>$fname</td>";
          echo"<td>$lname</td>";
          echo"<td>$customer_id</td>";
          echo"<td><input type='hidden' name='customer_id' value='$customer_id'>";
          echo "<button type='submit' name='submit'>More Info</button></td>" ; 
          }
          echo" </tr>";
                ?>
   </tbody>
</table>
</form>

With the second part of the code, I want to grab the customer_id when the button is clicked and display it below the table(I am going to uses the variable in a subsequent query to display more information later). The button selects the same customer_id no matter which button is clicked, and it is always the last record in the table. I would appreciate any help in correcting my error.

<?php
$ccf=" ";
if(isset($_POST['submit'])){

  $ccf = $_POST['customer_id'];
}
echo "Customer Id selected is:$ccf";
?>
2
  • 1
    These seem to be all in a single form, so there are multiple elements with name='customer_id'. Place each row inside its own form Commented Aug 22, 2021 at 11:18
  • 1
    Done, glad it works ;) Commented Aug 22, 2021 at 12:04

1 Answer 1

1

Your elements are all inside one form, so there are multiple elements with name='customer_id'. Place each row inside their own separate <form></form>:

<table>
   <thead>
      <tr>
         <th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
      </tr>
   </thead>
   <tbody>
         <?php 
         $sql = "select fname, lname,customer_id from customer_address";
         $result = mysqli_query($conn,$sql);

         while($row = mysqli_fetch_assoc($result)){
            $fname = $row['fname'];
            $lname = $row['lname'];
            $customer_id = $row['customer_id'];
            
            echo" <tr>";
            echo "<td>$fname</td>";
            echo "<td>$lname</td>";
            echo "<td>$customer_id</td>";
            echo "<td>";
            echo "<form action='' method='post'>";
            echo "<input type='hidden' name='customer_id' value='$customer_id'>";
            echo "<button type='submit' name='submit'>More Info</button>";
            echo "</form>";
            echo "</td>"; 
            echo" </tr>";
          }
          ?>
   </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.