I am creating a form to either approve or deny a time-off request. So this will bring up a table with rows of data of time off requests. You check the box and on submitting the form I want it to change the Status of the row AND send an email to the user letting them know it has been accepted. I have it updating the row, but when it sends the email it is using the information from the LAST row of data in the table. So I have a PHP form containing the following:
<form name="bulk_action_form" action="" method="post"/>
<tr>
<th></th>
<th>Username</th>
<th>Employee Name</th>
<th>First Day Off</th>
<th>First Day Back</th>
<th>Status</th>
<th>Date of Request</th>
</tr>
</thead>
<?php
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_assoc($query)){
?>
<tr>
<td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['ID']; ?>"/>
<input type="hidden" name="worker_name" value="<?php echo $row['worker_name']; ?>"/>
<input type="hidden" name="Name" value="<?php echo $row['Name']; ?>"/>
<input type="hidden" name="FirstDayOff" value="<?php echo $row['FirstDayOff']; ?>"/>
<input type="hidden" name="FirstDayBack" value="<?php echo $row['FirstDayBack']; ?>"/></td>
<td><?php echo $row['worker_name']; ?></td>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['FirstDayOff']; ?></td>
<td><?php echo $row['FirstDayBack']; ?></td>
<td><?php echo $row['Status']; ?></td>
<td><?php echo $row['_sfm_form_submision_time_']; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="5">No records found.</td></tr>
<?php } ?>
<input type="submit" class="btn btn-success" name="bulk_approve_submit" formaction="action-approve.php" value="Approve Request">
</form>
And my action-approve.php looks as follows.
<?php
session_start();
include_once('dbConfig.php');
if(isset($_POST['bulk_approve_submit'])){
$idArr = $_POST['checked_id'];
foreach($idArr as $id){
mysqli_query($conn,"UPDATE Request_off SET Status='Approved' WHERE ID=".$id);
// Get Parameters about the row that was selected
$worker_name = $_POST['worker_name'];
$Name = $_POST['Name'];
$FirstDayOff = $_POST['FirstDayOff'];
$FirstDayBack = $_POST['FirstDayBack'];
// Email Parameters
$to = $worker_name.'@somewhere.com, [email protected]';
$subject = 'APPROVED: Time off request for '.$Name;
$message = 'Dear '.$Name.','. "\r\n\n" .'Your time off request from '.$FirstDayOff.' to '.$FirstDayBack.' has been approved!'. "\r\n\n" .'Note: Please keep a copy of this email for your records.';
$headers = 'From: [email protected]';
mail($to, $subject, $message, $headers);
}
$_SESSION['success_msg'] = 'Request has been approved successfully.';
header("Location: Request_off-admin.php?Status=Pending");
}
?>
How would I go about getting the row data from the checked row, NOT the last row in the particular table?
UPDATE:
I just took those hidden fields and turned them into checkboxes. If I check all of the boxes for a particular row, I can make it update and send the email correctly. I'm going to look into hiding those checkboxes and using just one to select them all...
$to = $worker_name.'@somewhere.com, [email protected]';that is part of the problem here.To:expects the email to be sent to the email row, or is that just pseudo code? Seeing the edit,<form>defaults to a GET method (and as self) and you're using POST arrays.