I am trying to create a "Mark as Dispatched" tool for a small Shopping Cart I am creating as part of an educational project.
The first page I have allows you to tick two boxes, one to get the Order ID and the second to retrieve the Customer's Email. The second page has the following code: (Please see the attached image for all of the code).
$orders = implode(",", $_POST['chkBoxID']);
$emails = implode(",", $_POST['chkBoxEmails']);
echo $orders;
echo $emails;
If I have two orders in my Database it will currently echo something out like this...
1,2,[email protected],[email protected]
How can I assign Order ID 1 to [email protected] and Order ID 2 to [email protected]?
The reason behind me doing this is so I can send an email with a list of emails in the $to = "" field and also attach the Order ID which is assigned to the email in order to show the customer which order has been dispatched.
If this is confusing - (sorry), I have attached an image to help explain what I am trying doing.
Full code:
mark-orders-dispatched.php
<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
<?php
foreach ($orders as $row) {
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<strong>Username:</strong> <input type='text' name='Username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
echo "</td>";
echo "<td>";
echo '<input type="checkbox" name="chkBoxID[]" id="chkBox" value="'. $row['OrderID'] .'"><input type="checkbox" name="chkBoxEmails[]" id="chkBox" value="'. $row['Username'] .'">';
echo "</td>";
echo "</tr>";
}
?>
<span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',true)">CHECK ALL</a></span>
<span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',false)">UNCHECK ALL</a></span>
<input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>
update-to-dispatched.php
<?php
include_once("../../config.php");
try {
$status = "Dispatched";
$orders = implode(",", $_POST['chkBoxID']);
$emails = implode(",", $_POST['chkBoxEmails']);
$info = array_combine($orders, $emails);
$stmt = "UPDATE orders SET Status = :status WHERE OrderID IN (:orders)";
$stmt = $conn->prepare($stmt);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':orders', $orders);
$stmt->execute();
foreach($info as $orderId => $email) {
$to = "". $email ."";
$subject = "Your order has been dispatched";
$message = "Thank you for your purchase!\n\nYour Order (Order ID: ". $orderId .") has been dispatched.\n\nIf you have any queries regarding your order, please reply to this email or use the Live Support system available from our website.";
$headers .= "From: REMOVED";
mail($to, $subject, $message, $headers);
}
header('Location: REMOVED');
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>