Jump to content

Submit button in while-loop


stirrah

Recommended Posts

Hey!

 

How should I do if I want to populate a table with results from DB, and then make a submit button beside each row..and then make a query depending on the values of that row.

 

This is my code right now:

if (isset($_POST['submit'])){
  // do some query to the db depending on row
}



	?>
      <h2>Accept or decline your invitation:</h2>
      <?php 
$my_id = $user_data['user_id'];
$query = mysql_query(MY QUERY) ?>

     <table class='display'>
             <th>ID</th>
              
              <th>Namn</th>
              <th>Inbjuden av</th>
              <th>Svara</th>
              <?php
                  while($row = mysql_fetch_assoc($query)) 
                  { ?>
		    <tr>
        <td><?php echo "<a href='project.php?pid=" . $row['projectId'] . "'>" . $row['projectId'] . "</a>"; ?></td> 
		    <td><?php echo $row['projektName'] ?></td>
        <td><?php echo $row['username'] ?></td>
        <td><form action="" method="POST"><input type="submit" name="submit" value="Accept!"></form></td>
		    </tr>
		
<?php } ?>

Problem with this setup is that it will submit all the rows before aswell..

Link to comment
https://forums.phpfreaks.com/topic/284304-submit-button-in-while-loop/
Share on other sites

To accept a specific database entry, the form needs to be updated to pass that entry's ID. If you want to stick with HTML forms, you could add the ID to a hidden field. If you're not familiar with creating hidden fields, perhaps the following will help:

http://www.tizag.com/htmlT/htmlhidden.php

 

 

Note that you could also use an HTML link. For example, you could try something like this:

<td><?php echo "<a href='?accept=" . $row['projectId'] . "'>Accept</a>"; ?></td>

Just keep in mind that the information passed through links are GET variables. So you would need to use $_GET.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.