这是我的脚本和我的电子邮件脚本。它会向我发送购物车,但如果我有两个以上的物品,它会以空白电子邮件的形式出现。电子邮件 php:
<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
$emailSubject = 'Order';
$webMaster = 'MY EMAIL;
{
$body = showCart();
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$success = mail ($webMaster, $emailSubject, $body, $headers)
/* Results rendered as HTML */
$theResults = <<<EOD
<html><body background="images/sand.jpg">
<table align="center" width="200px" height="100px" bgcolor="#0099FF" style="border:solid medium #0F0; border-radius:4px; margin-top:200px">
<tr>
<td><h3 align="center">Your message has been sent.</h3></td>
</tr>
<tr>
<td><h4 align="center">Redirecting now...</h4></td>
</tr>
</table>
</body></html>
EOD;
echo "$theResults";
}
?>
购物车脚本 此购物车工作正常,只是无法正确通过电子邮件:
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
$output[] = '<tr>';
$output[] = '<thead>';
$output[] = '<th></th>';
$output[] = '<th>Item</th>';
$output[] = '<th>Price</th>';
$output[] = '<th>Quantity</th>';
$output[] = '<th>Total</th>';
$output[] = '</thead>';
$output[] = '</tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>$'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>$'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$tax = .07;
$taxtotal += round($total * $tax,2);
$amounttotal += $total + $taxtotal;
$output[] = '<p>Tax: <strong>$'.$taxtotal.'</strong></p>';
$output[] = '<p>Total: <strong>$'.$amounttotal.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
$output[] = '<form action="send.php" method="post" enctype="multipart/form-data">';
$output[] = '<div><button type="submit">Check Out</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>