Jump to content

I have made a cart in php, but dont know how to send it to an email address


andrej13

Recommended Posts

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 &#0183; 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
Share on other sites

to email it, you can use PHP's mail() function

mail('youraddress@domain.com', 'Subject', $cart, 'From: Sender Name <sender@domain.com>');

 

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
Share on other sites

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('youraddress@domain.com', 'Subject', $cart, 'From: Sender Name <sender@domain.com>');
}

 

If you place a filename in action="", you will be able to include the PHP in that file instead.

Link to comment
Share on other sites

Still does not work :s , but thanks anyway

 

this is now my mail.php

<?php

if($_POST['sendemail'] == 'Email')
{
  mail('andrej13@gmail.com', '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 &#0183; 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
Share on other sites

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 <address@domain.com>';
  mail('youraddress@domain.com', '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
Share on other sites

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 <address@domain.com>';
  mail('andrej13@gmail.com', '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 &#0183; 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('andrej13@gmail.com', 'Subject', $cart);
}


?>
<p><a href="index.php">Back to bookshop...</a></p>



</div>

</body>
</html>

 

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.