You have problems viewing the code.
1. ids can only be used once per HTML page
HTML identifiers are unique and can only be used once per page. that is your first design mistake, you need to use classes instead of ids if you generate html inside a PHP loop.
2. the id sent by ajax should depend on where you click
you are currently always sending the same identifier to your controller because $('#JobId').html() independently returns the first #JobId html. you need to find out which button your user clicked to be able to distinguish between your identifiers.
1. step - replace id with classes in html for all elements that are displayed more than once. I also added the data-id attribute to your button, so itβs very easy to find out which button was pressed:
<tr> <td class="hiringevent"><?php echo $JobResults['hiringevent']; ?></td> <td class="JobId"><?php echo $JobResults['id_job']; ?></td> <td><button class="ViewDetails" class="btn btn-primary text-center" data-id="<?php echo $JobResults['id_job']; ?>">View Details</button></td> </tr>
2. step - in your ajax request: find out which button was clicked by the user - I use the data attribute here:
// ... data: {'action':'viewjob','JobId' : $(this).data('id')}, // ...
Explanation: $(this) in jquery always refers to the current element that raised the event. in your case, this is a click event that was triggered by your button - therefore $(this).data('id') reads the data attribute "id" in your button HTML code.
source share