Jump to content

Sending details of array by email


gold2040

Recommended Posts

Hi guys

 

I have a basic shopping cart here, taken from a book labeled "Build Your Own Database Driven Web Site Using PHP & MySQL"

 

basic catalogue

 

<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
if (isset($_GET['buy'])) {
// Add item to the end of the $_SESSION['cart'] array
$_SESSION['cart'][] = $_GET['buy'];
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Product catalog</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Your shopping cart contains <?php
echo count($_SESSION['cart']); ?> items.</p>
<?php
$items = array(
'Canadian-Australian Dictionary',
'As-new parachute (never opened)',
'Songs of the Goldfish (2CD Set)',
'Ending PHP4 (O\'Wroxey Press)');
$prices = array(24.95, 33.95, 19.99, 34.95);
?>
<table border="1">
<thead>
	<tr>
		<th>Item Description</th>
		<th>Price</th>
	</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < count($items); $i++) {
	echo '<tr>';
	echo '<td>' . $items[$i] . '</td>';
	echo '<td>$' . number_format($prices[$i], 2) . '</td>';
	echo '<td><a href="' . $_SERVER['PHP_SELF'] .
		'?buy=' . $i . '">Buy</a></td>';
	echo '</tr>';
}
?>
</tbody>
</table>
<p><a href="cart.php">View your cart</a></p>
</body>
</html>

 

and basket

 

<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
if (isset($_GET['empty'])) {
unset($_SESSION['cart']);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
if (isset($_GET['delete'])) {
$delete = $_GET['delete'];
array_splice($_SESSION['cart'], $delete, 1);
header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID);
exit();
}
?>







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Shopping cart</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Your Shopping Cart</h1>
<?php
$items = array(
'Canadian-Australian Dictionary',
'As-new parachute (never opened)',
'Songs of the Goldfish (2CD Set)',
'Ending PHP4 (O\'Wroxey Press)');
$prices = array( 24.95, 33.95, 19.99, 34.95 );
$shipping = array( 1.95 );
?>
<table border="1">
<thead>
	<tr>
		<th>Item Description</th>
		<th>Price</th>
	</tr>
</thead>
<tbody>
<?php
$total = 0;
for ($i = 0; $i < count($_SESSION['cart']); $i++) {
		echo '<tr>';
		echo "<td><div align='center'><a href=\"$PHPSELF?delete=$i\">Delete</a>";							
		echo '<td>' . $items[$_SESSION['cart'][$i]] . '</td>';
		echo '<td align="right">$';
		echo number_format($prices[$_SESSION['cart'][$i]], 2);
		echo '</td>';
		echo '</tr>';
		$total = $total + $prices[$_SESSION['cart'][$i]];

}
?>
</tbody>
<tfoot>
    <tr>
	<th align="right"></th><br>
	<th align="right">Total: $<?php echo number_format($total, 2); ?>
    </th>
</tr>
</tfoot>
</table>







<p><a href="catalog.php">Continue Shopping</a> or
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?empty=1">Empty your
cart</a></p>
    

</body>
</html>

 

what i'd like to do is to send the data which is stored in the array (items and total cost) via email.when added to the cart

fairly new at this but what I gather, it should be rather simple, kinda urgent I figure this out :(, pulling my hair out

 

here's a link to this online if it helps - http://localhost/webcart/catalog.php

Link to comment
https://forums.phpfreaks.com/topic/186101-sending-details-of-array-by-email/
Share on other sites

For one we cant view that URL. Its LAN side only. Not on the web.

 

Im sure the user is going to get pissed if they get a E-Mail everytime they add something to the cart. If they are like, I add and remove items from my cart 8 to 10 times before I check out. (Always finding something better) Point is, thats 8 to 10 e-mails before they checked out. If im not reading what you are asking wrong.

 

You can google for a Email script.

 

<?php

$to = "[email protected]";

$subject = "Hi!";

$body = "Hi,\n\nHow are you?";

if (mail($to, $subject, $body)) {

  echo("<p>Message successfully sent!</p>");

} else {

  echo("<p>Message delivery failed...</p>");

}

?>

 

Make the $body say anything you want.

apologies for not being clearer, I meant when the user wants to checkout,, not every time a product was added to the cart

 

here's the web link - http://pspdosbox.co.uk/shoppingcart/catalog.php

 

it's some University work, so i'm only looking to have a basic functional cart

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.