I created a table in a form that dynamically generates input fields with names based on a location id. Now I want to post that to a different file that emails the information using PHPMailer. I learned from other posts here to use array posting, such as name="name[]"
, but now I'm stuck on how to add each item to the body of the PHPMailer email.
Here is what my table in my form looks like:
<table class="coach_log">
<?php
$query = "SELECT location_id FROM users WHERE id = " . $_SESSION['user_id'];
$result = mysql_query($query, $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$location_id = $row["location_id"];
$query = "SELECT first_name, last_name FROM users WHERE location_id = " . $location_id . " ORDER BY first_name";
$result = mysql_query($query, $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$name = $row['first_name'] . " " . $row['last_name'];
echo ' <tr>
<td colspan="5" style="padding: 0; font-size: 20px; font-weight: 700;">' . $name . '</td>
</tr>
<input type="text" name="name[]" value="' . $name . '">';
}
}
?>
</table>
And here's an example of how it is handled by my separate file that produces the email:
$name = $_POST['name'];
$htmlBody = "
<table>
<tr>
<td style=\"padding: 5px !important; text-align: left; line-height: 25px; font-family: sans-serif; font-size: 14px;\" bgcolor=\"#FFFFFF\">{$name}</td>
</tr>
</table>";
How do I get the $htmlBody to generate the all the additional rows created in the form?
Using foreach works really well, but I have multiple inputs under each name that need to be sent like this:
foreach ($names as $name) {
$htmlBody .= "
<tr>
<td>{$name}</td>
<td>{$today_actual}</td>
<td>{$today_goal}</td>
<td>{$month_actual}</td>
</tr>
They all belong together. But if I do a separate foreach on each one I'd get the information in the wrong order. Any ideas?
// Make sure we're dealing with an array of names
$names = is_array($_POST['name']) ? $_POST['name'] : array($_POST['name']);
$htmlBody = "..."; // Header HTML
foreach ($names as $name) {
$htmlBody .= "<tr><td>$name</td></tr>";
}
$htmlBody .= "..."; // Footer HTML
$names= array_values($_POST['name']);
foreach($names as $name) {
$htmlBody= $htmlBody . $name;
}