andrej13 Posted January 23, 2011 Share Posted January 23, 2011 I have made a shopping cart where I can add items from my sql table. But I dont know how to send the table to my email. this is my cart.php Can someone give me some tips? Thanks in advance. you can also check it out on http://fhcs.be/cart-demo3/ <?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(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!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" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo · Cart</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php echo showCart(); ?> <p><a href="index.php">Back to bookshop...</a></p> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/ Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 to email it, you can use PHP's mail() function mail('[email protected]', 'Subject', $cart, 'From: Sender Name <[email protected]>'); Is that sufficient, or do you need to do something else? Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164063 Share on other sites More sharing options...
andrej13 Posted January 23, 2011 Author Share Posted January 23, 2011 to email it, you can use PHP's mail() function mail('[email protected]', 'Subject', $cart, 'From: Sender Name <[email protected]>'); Is that sufficient, or do you need to do something else? Is there any way to make a function out of it and using a send button? Thanks for the help Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164065 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 Is there any way to make a function out of it and using a send button? Thanks for the help Yes, You could add the following html: <form action="" method="post"> <input type="submit" name="sendemail" value="Email" /> </form> and the following PHP: if($_POST['sendemail'] == 'Email') { mail('[email protected]', 'Subject', $cart, 'From: Sender Name <[email protected]>'); } If you place a filename in action="", you will be able to include the PHP in that file instead. Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164076 Share on other sites More sharing options...
andrej13 Posted January 23, 2011 Author Share Posted January 23, 2011 Still does not work :s , but thanks anyway this is now my mail.php <?php if($_POST['sendemail'] == 'Email') { mail('[email protected]', 'Subject', $cart); } ?> and this is my cart.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 require_once('mail.php'); session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!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" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo · Cart</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php echo showCart(); ?> <form action="mail.php" method="post"> <input type="submit" name="sendemail" value="Email" /> </form> <p><a href="index.php">Back to bookshop...</a></p> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164106 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 Still does not work :s , but thanks anyway What is the exact problem you get? Does the mail deliver? You don't have any header information, such as a From address, which may cause you problems. Try: if($_POST['sendemail'] == 'Email') { $headers = 'From: Sender <[email protected]>'; mail('[email protected]', 'Subject', $cart, $headers); echo 'Your mail has been sent'; } else { echo 'Your mail was not sent'; } The echoed text will tell you if the mail function is being called. Also, it's best not to put your real email address in forum posts, as it will increase the likelihood of you receiving spam in the future. Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164112 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 Ah, just realised that $cart is not defined in mail.php You will need to define it in mail.php as well: $cart = $_SESSION['cart']; Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164113 Share on other sites More sharing options...
andrej13 Posted January 23, 2011 Author Share Posted January 23, 2011 Ah, just realised that $cart is not defined in mail.php You will need to define it in mail.php as well: $cart = $_SESSION['cart']; I dont receive the mail. although the echo says that the mail is sent mail.php <?php if($_POST['sendemail'] == 'Email') { $cart = $_SESSION['cart']; $headers = 'From: Sender <[email protected]>'; mail('[email protected]', 'Subject', $cart, $headers); echo 'Your mail has been sent'; } else { echo 'Your mail was not sent'; } $cart = $_SESSION['cart']; ?> cart.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 require_once('mail.php'); session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!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" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo · Cart</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php echo showCart(); ?> <form action="mail.php" method="post"> <input type="submit" name="sendemail" value="Email" /> </form> <?php if($_POST['sendemail'] == 'Email') { mail('[email protected]', 'Subject', $cart); } ?> <p><a href="index.php">Back to bookshop...</a></p> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164115 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 I dont receive the mail. although the echo says that the mail is sent It might be that gmail is sifting the message out. Have you checked your junkmail? Also, do you have another address you can send to? (preferably not @gmail) If it fails to send to other addresses, it may be something with the php installation/configuration. If you have a web-host, you may need to speak to them. Not sure I can offer any more advice than that, as the code appears correct. Link to comment https://forums.phpfreaks.com/topic/225420-i-have-made-a-cart-in-php-but-dont-know-how-to-send-it-to-an-email-address/#findComment-1164134 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.