Forum Moderators: coopster
The coupons are selected from a MySQL database and echoed in a while loop, thus:
while($row = mysql_fetch_assoc($result)) {
echo "<table>";
echo "<tr>";
echo "<td rowspan=\"2\"><img src=\"images/" . $row['advlogo'] . "\" alt=\"\" /></td>";
echo "<td><a href=\"" . $row['gotourl'] . "\">" . $row['anch'] . "</a><br />";
echo (empty($row['code']) ? $row['instr'] : $row['code']);
echo "<br /> Expires: ";
echo (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
echo "<br /> About <a href=\"" . $row['store'] . "\">" . $row['adv'] . "</a><br />";
echo "Category: <a href=\"" . $row['category'] . "\">" . $row['cat'] . "</a></td>";
echo "<td rowspan=\"2\"><a href=\"" . $row['gotourl'] . "\">" . $row['blockanch'] . "</a></td>";
echo "</tr>";
echo "<tr>";
echo "<td>
<form action=\"email-coupons.php?id=" . $row['id'] ."\" method=\"post\">
<input type=\"image\" src=\"images/emailcpn.png\" />
</form>
</td>";
echo "</tr>";
echo "</table>";
} // end while
Note the form towards the end. It lets me post coupon IDs to email-coupons.php.
On email-coupons.php, I get the ID, select the coupon and, as before, echo it in a while loop. I also ask for the To and From e-mail addresses, thus:
<?php
$id = intval($_GET['id']);
$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);
$select = "SELECT
adv,
instr,
code,
bmurl,
anch,
DATE_FORMAT(expdate, '%M %D, %Y') AS formated_date,
expun
FROM tadv, tinstr, tc
LEFT JOIN texpun on tc.expunid=texpun.expunid
WHERE
tc.instrid=tinstr.instrid AND
tc.advid=tadv.advid AND
tc.id=$id";
$query = mysql_query($select);
while($row = mysql_fetch_assoc($query)) {
echo "<table>;
echo "<tr>";
echo "<td>";
echo "Offer: <a href=\"" . $row['bmurl'] . "\">" . $row['anch'] . "</a><br />";
echo "Store: " . $row['adv'];
echo "<br /> Expires: ";
echo (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
echo "<br /> Instructions: ";
echo (empty($row['code']) ? $row['instr'] : $row['code']);
echo "</td>";
echo "</tr>";
ech "</table>";
echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
echo "Send to:<br />";
echo "<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea>";
echo "From:<br />";
echo "<input size=\"25\" name=\"from\" />";
echo "<input type=\"submit\" name=\"send\" value=\"Send\" />";
echo "</form>";
}
?>
This configuration lets me send the To and From e-mail addresses to email-coupons-manager.php - but not the coupon itself! How can I put the coupon into a variable to send it, keeping in mind that it's echoed in a while loop?
Here's what I've got on email-coupons-manager.php:
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$header = "From: $from";
$subject = "Check Out this Coupon!";
if ($to == '') {print "<p>You have not entered the recipient's e-mail.</p>";}
elseif ($from == '') {print "<p>You have not entered your e-mail.</p>";}
else {
$send = mail($to, $from, $header, $subject);
if($send) {print "<p>Thank you!</p>";}
else {print "<p>An error occurred.</p>";}
}
?>
echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"id\" value=\"".$row['coupon_id']."\">"; // This line
echo "Send to:<br />";
echo "<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea>";
echo "From:<br />";
echo "<input size=\"25\" name=\"from\" />";
echo "<input type=\"submit\" name=\"send\" value=\"Send\" />";
echo "</form>";
Then in email-coupons-manager.php take $_POST['id'] and load the coupon info just as you do in the other pages.
If you're going to put HTML in the value of a hidden form tag, I recommend that you encode it first. This will stop browsers getting confused, displaying things wrong and giving you unexpected results when the form is posted. base64_encode is good for this.
echo '<input type="hidden" name="my_string" value="'.base64_encode($my_string).'">';
Then on the processing page:
$my_string = base64_decode($_POST['my_string']);