0

I have written a php code to display data as a table, using the values come from a MySQL database. I am adding 2 buttons to end of each row, 'Approve' and 'Reject' for example. It's something like this:

<?php
$con2=mysqli_connect ("localhost","root","","leavemanagement");
$res=mysqli_query($con2,"SELECT c.* , p.* FROM leaveapplication c,emp p WHERE c.empno=p.empno");
while($row=mysqli_fetch_array($res))
{
?>
<tr>
<td><p><?php echo $row['empno']; ?></p></td>
<td><p><?php echo $row['name']; ?></p></td>
<td><p><?php echo $row['dept']; ?></p></td>
<td><p><?php echo $row['startdate']; ?></p></td>
<td><p><?php echo $row['enddate']; ?></p></td>
<td><p><?php echo "<input type='submit' name='action' value='Approve' /><input type='submit' name='action' value='Reject' />" ?></p></td>
</tr>
<?php
}
?>

How can I perform an action based on each button click? For example if the user clicks 'Approve' button on 4th row, it should pick 'empno', 'startdate', 'enddate' of that row, so I can pass that data to update the database.

1
  • 1
    just have a unique name for each row's action buttons. It's ok to have the same name for both the approve and reject button on each row Commented Sep 9, 2017 at 16:13

2 Answers 2

2

Easiest way would be to transform each <tr> block into a form.

Example:

...
<tr>
   <form ...>
      <td><p><?php echo $row['empno']; ?></p></td>
      <td><p><?php echo $row['name']; ?></p></td>
      <td><p><?php echo $row['dept']; ?></p></td>
      <td><p><?php echo $row['startdate']; ?></p></td>
      <td><p><?php echo $row['enddate']; ?></p></td>
      <td><p><?php echo "<input type='submit' name='action' value='Approve' /><input type='submit' name='action' value='Reject' />" ?></p></td>
   </form>
</tr> 
...

Edit: For tracking you can use another <input type="hidden" value="..."> field with a relevant value for that row

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I need something along your suggestion.Here's the latest and I know well why it returns the last value but still failing to think of a fix '$i=0; while($row=mysqli_fetch_array($res)) { $testeno=$row['eno']; ?> <tr> <form method="post" action="..."> <td><p><?php echo $row['eno']; ?><input type="hidden" name="eno" value="<?php echo $row['eno'];?>"></p></td> <td><p><?php echo "<input type='submit' name='approve' value='Approve' /></p></td> </form> </tr> <?php $i++; } ?> </table> <?php echo $testeno;'
Could someone kindly tell me what's the recommended way to add secondary info, as the above comment for example, where I need more than 600 characters to explain it?
0

You're being shown an unnecessary use of forms. Make the TR have an ID equal to the row number. Then use an AJAX request using the row number and button pressed (accept or reject) to make the desired change. I'm on my phone now but I can add an example of this later if you'd like.

3 Comments

Wasn't sure he reached AJAX on his study list. that's why the solution offered is easier.
Just because something is easier doesn't mean it's the best solution. Besides, disregarding knowing or not knowing what he has studied, some people learn best through example.
Under different circumstances I'd agree with your view but in this case, I need a very basic answer limited to html and php as @bogdan_perian has suggested.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.