This project is with E-Commerce. There is option to Add to Cart. When user click on add, product will be added to his cart as well and working fine.
But finally when user click place order button Admin will receive Mail with products. That E-mail body should content should display like below.
I tried with using foreach and echo data row by row. But its useless .
Question is - I want retrieve data from cart and add to Above table Format and then it will end with mailing. How to archive this with CI?
Html Table Format
$to = 'mail#gmail.com';
$msg .= '
<table id="table" border="0" cellpadding="5px" cellspacing="1px" style="border: 1px solid #eee">
<tr id= "main_heading">
<td>Name</td>
<td>Price</td>
<td>Qty</td>
<td>Amount</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>';
Note : there is no special code to this(inserting and updating). all the codes are same with ellislab.com
CART VIEW
If i use
$as = $this->cart->contents();
print_r($as);
it shows
Controller
public function index()
{
$data['category'] = $this->Product_Model->get_category();
$data['category2'] = $this->Product_Model->get_category2();
$data['product'] = $this->Product_Model->get_product();
$data['right_brand'] = $this->Product_Model->get_right_brand();
$data['right_prod'] = $this->Product_Model->get_right_product();
$data['brand'] = $this->Product_Model->get_brand_names();
$data['products'] = $this->Product_Model->get_cart_items();
$data['head'] = $this->Product_Model->check_cart();
$this->load->view('template/header', $data);
$this->load->view('template/right_sidebar', $data);
$this->load->view('pages/cart', $data);
$this->load->view('template/foot');
}
public function insert_cart()
{
$data = array(
'id' => $this->input->post('pid'),
'qty' => $this->input->post('qty'),
'price' => $this->input->post('price'),
'name' => $this->input->post('head'),
);
$this->cart->insert($data);
}
function remove($rowid)
{
if ($rowid==="all")
{
$this->cart->destroy();
redirect('index.php/main');
}
else
{
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
// This will show cancle data in cart.
redirect('index.php/cart');
}
function update_cart()
{
$cart_info = $_POST['cart'] ;
foreach( $cart_info as $id => $cart)
{
$rowid = $cart['rowid'];
$qty = $cart['qty'];
$data = array(
'rowid' => $rowid,
'qty' => $qty
);
$this->cart->update($data);
}
redirect('index.php/cart');
}
use jquery and capture the html table that you want to send as email. and pass the variable to the email function using ajax, try this,
<script>
$(document).ready(function() {
var htmlstring = $("#mail_table").html();
$( "#send" ).click(function() {
var cn_name = $("#cn_name").val();
var cn_mail = $("#cn_mail").val();
$.ajax({
method : 'post',
dataType : 'html',
url :'<?php echo base_url(); ?>index.php/cart/testmail',
data :{
name:cn_name,
mail:cn_mail
table:htmlstring
}
});
});
});
</script>
jquery veriable = htmlstring
also you can use jquery serialize method to get all form inputs.
https://api.jquery.com/serialize/
CI ocontroller
public function testmail(){
$to = 'mail#gmail.com';
$subject = 'Enquiry from ';
$message = $_POST['table'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to, $subject, $message, $headers))
{
}
}
For view cart and receive it as mail use this...
In mail use this code...
$tpl_file = 'cartmail.php';
if (!file_exists($tpl_file)) {
die('Mail could not be sent');
exit;
}
$msg_tmpl = file_get_contents($tpl_file);
$to = 'mail#gmail.com';
$subject = 'Enquiry from ' .$_REQUEST['guest_name'];
$message = $msg_tmpl;
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$_REQUEST['email'];
mail($to, $subject, $message, $headers);
And in the cartmail.php file use this code
<div class="table-responsive">
<table class="shop_table cart" cellspacing="0">
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-thumbnail"> </th>
<th class="product-name">Product</th>
<th class="product-price">Price</th>
<th class="product-quantity">Quantity</th>
<th class="product-subtotal">Total</th>
</tr>
</thead>
<tbody>
<?php
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
?>
<tr class="cart_item">
<td class="product-name">
<span class="name"><?php echo $obj->product_name; ?> </span> </td>
<td class="product-price">
<span class="amount"><?php echo $currency,$obj->price;?></span> </td>
<td class="product-quantity">
<div class="quantity"><?php echo $cart_itm["qty"];?></div>
</td>
<td class="product-subtotal">
<span class="amount"><?php $ptotal=$obj->price*$cart_itm["qty"]; echo $ptotal; ?></span> </td>
<td class="product-remove">
<?php echo '×';?> </td>
</tr>
<?php
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
$cart_items ++;
}
?>
</tbody>
</table>
</div>
Related
I have a PhP shopping cart for autoparts and I need to capture all the chosen products and send them to my email together with a contact form upon clicking "Submit order". As of now I've succeeded at sending just the first chosen product but I need to capture all the chosen products not just one.
I tried capturing the array itself with all the atributes of the chosen products but it's sending me only the first chosen product.
PhP
<?php
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM mystuff WHERE mscode='" . $_GET["mscode"] . "'");
$itemArray = array($productByCode[0]["mscode"]=>array('mscategory'=>$productByCode[0]["mscategory"], 'mscatnum'=>$productByCode[0]["mscatnum"], 'msnomer'=>$productByCode[0]["msnomer"], 'msmark'=>$productByCode[0]["msmark"], 'msmodel'=>$productByCode[0]["msmodel"], 'msyear'=>$productByCode[0]["msyear"],'mscode'=>$productByCode[0]["mscode"], 'quantity'=>$_POST["quantity"], 'msprice'=>$productByCode[0]["msprice"], 'msimage'=>$productByCode[0]["msimage"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["mscode"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["mscode"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
HTML
<HTML>
<HEAD>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</HEAD>
<div class="bodyr">
<div id="shopping-cart">
<div class="txt-heading"></div>
<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
?>
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;" width="2%">Category</th>
<th style="text-align:left;" width="2%">Category №:</th>
<th style="text-align:left;" width="2%">Stock №:</th>
<th style="text-align:left;" width="2%">Mark:</th>
<th style="text-align:left;" width="2%">Model:</th>
<th style="text-align:left;" width="2%">Year:</th>
<th style="text-align:left;" width="2%">Quantity:</th>
<th style="text-align:left;" width="2%">Price:</th>
<th style="text-align:left;" width="2%">Total price:</th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
$item_price = $item["quantity"]*$item["msprice"];
?>
<tr>
<td><?php echo $item["mscategory"]; ?></td>
<td><?php echo $item["mscatnum"]; ?></td>
<td><?php echo $item["msnomer"]; ?></td>
<td><?php echo $item["msmark"]; ?></td>
<td><?php echo $item["msmodel"]; ?></td>
<td><?php echo $item["msyear"]; ?></td>
<td style="text-align:left;"><?php echo $item["quantity"]; ?></td>
<td style="text-align:left;"><?php echo "$ ".$item["msprice"]; ?></td>
<td style="text-align:left;"><?php echo "$ ". number_format($item_price,2); ?></td>
</tr>
<?php
$total_quantity += $item["quantity"];
$total_price += ($item["msprice"]*$item["quantity"]);
}
?>
<tr>
<td colspan="7" align="left"> <div style="font-weight:bold; font-size:14px"> Total:</div></td>
<td align="left"><?php echo $total_quantity; ?></td>
<td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td>
</tr>
</tbody>
</table>
<?php
} else {
?>
<div class="no-records">Your cart is empty</div>
<?php
}
?>
</div>
<form action="" method="post">
<input type="text" name="namet" placeholder="Name: *" required>
<input type="tel" name="adrphonenumbert" placeholder="Phone number: *" required>
<input type="email" name="emailt" placeholder="E-mail: *" required>
</form>
</html>
PhP Mail() function
<?php
$response = '';
$subject = 'Autoparts';
if (isset($_POST['namet'], $_POST['phonenumbert'], $_POST['emailt'] )) {
if (!filter_var($_POST['emailt'], FILTER_VALIDATE_EMAIL)) {
$response = 'The e-meil address is not valid!';
} else if (empty($_POST['namet']) || empty($_POST['phonenumbert']) || empty($_POST['emailt'])) {
$response = 'Please, fill all the fields.';
} else {
$namet = $_POST['namet'];
$phonenumbert = $_POST['phonenumbert'];
$emailt= $_POST['emailt'];
$to = 'LLstdz#gmail.com';
foreach ($_SESSION["cart_item"] as $itemArray){
if ($itemArray > 0){
$txt = '<h2>Autoparts purchase:</h2>
<p><b>Name:</b> '.$namet.'</p>
<p><b>Phone number:</b> '.$phonenumbert.'</p>
<p><b>E-meil:</b> '.$emailt.'</p>
<p><b>Category:</b><br/>'.$itemArray["mscategory"].'</p>;
<p><b>Category №:</b><br/>'.$itemArray["mscatnum"].'</p>;
<p><b>Stock №:</b><br/>'.$itemArray["msnomer"].'</p>;
<p><b>Mark:</b><br/>'.$itemArray["msmark"].'</p>;
<p><b>Model:</b><br/>'.$itemArray["msmodel"].'</p>;
<p><b>Year:</b><br/>'.$itemArray["msyear"].'</p>;
<p><b>Quantity:</b><br/>'.$itemArray["quantity"].'</p>;
<p><b>Price:</b><br/>'.$itemArray["msprice"].'</p>';
};
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $txt, $headers);
echo '<div style="font-weight:bold;font-size:20px;text-align:center">Thank you for using our services.</div>' ;
}
}
?>
This is because you should concatenate the string $txt, so use .= instead =
At the time you are sending only last item in $_SESSION["cart_item"] (not first)
for your case:
$txt = '';
foreach ($_SESSION["cart_item"] as $itemArray){
if ($itemArray > 0){
$txt .= '<h2>Autoparts purchase:</h2>
<p><b>Name:</b> '.$namet.'</p>
<p><b>Phone number:</b> '.$phonenumbert.'</p>
<p><b>E-meil:</b> '.$emailt.'</p>
<p><b>Category:</b><br/>'.$itemArray["mscategory"].'</p>;
<p><b>Category №:</b><br/>'.$itemArray["mscatnum"].'</p>;
<p><b>Stock №:</b><br/>'.$itemArray["msnomer"].'</p>;
<p><b>Mark:</b><br/>'.$itemArray["msmark"].'</p>;
<p><b>Model:</b><br/>'.$itemArray["msmodel"].'</p>;
<p><b>Year:</b><br/>'.$itemArray["msyear"].'</p>;
<p><b>Quantity:</b><br/>'.$itemArray["quantity"].'</p>;
<p><b>Price:</b><br/>'.$itemArray["msprice"].'</p>';
};
}
By the way, I strongly recommend to use phpmailer (https://github.com/PHPMailer/PHPMailer) or swiftmail for sending email.
I want to send the data from the order to the email + contact form send.
I do not know how to send cart data to an email with a contact form
function load_cart_data()
{
$.ajax({
url:"fetch_cart.php",
method:"POST",
dataType:"json",
success:function(data)
{
$('#cart_details').html(data.cart_details);
$('.total_price').text(data.total_price);
$('.badge').text(data.total_item);
}
});
}
Cart ....
<?php
//fetch_cart.php
session_start();
$total_price = 0;
$total_item = 0;
$output = '
<div class="table-responsive" id="order_table">
<table class="table table-bordered table-striped">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Total</th>
<th width="5%">Action</th>
</tr>
';
if(!empty($_SESSION["shopping_cart"]))
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$output .= '
<tr>
<td>'.$values["product_name"].'</td>
<td>'.$values["product_quantity"].'</td>
<td align="right">$ '.$values["product_price"].'</td>
<td align="right">$ '.number_format($values["product_quantity"] * $values["product_price"], 2).'</td>
<td><button name="delete" class="btn btn-danger btn-xs delete" id="'. $values["product_id"].'">Remove</button></td>
</tr>
';
$total_price = $total_price + ($values["product_quantity"] * $values["product_price"]);
$total_item = $total_item + 1;
}
$output .= '
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ '.number_format($total_price, 2).'</td>
<td></td>
</tr>
';
}
else
{
$output .= '
<tr>
<td colspan="5" align="center">
Your Cart is Empty!
</td>
</tr>
';
}
$output .= '</table></div>';
$data = array(
'cart_details' => $output,
'total_price' => '$' . number_format($total_price, 2),
'total_item' => $total_item
);
echo json_encode($data);
?>
email send
<?php
$namebusiness = $_POST['namebusiness'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$adress = $_POST['adress'];
$city = $_POST['city'];
$psc = $_POST['psc'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$data = $_POST['data'];
$message = $_POST['cart_details'];
$email_subject = "Order";
$email_body = "Name Of Business: $namebusiness.\n "."First name: $firstname.\n "."Last name: $lastname.\n"."E-mail: $visitor_email.\n"."Adress: $adress.\n "."City: $city.\n "."Post Code / ZIP: $psc.\n "."State: $state.\n "."Phone number: $phone.\n";
$to = "patrikl123#seznam.cz";
$to = $_POST['email'];
$headers = 'From: domaci#potrebyhanka.cz' . "\r\n" .
'Reply-To: domaci#potrebyhanka.cz' . "\r\n" .
'Content-type: text/html; charset=UTF-8' . "\r\n".
'X-Mailer: PHP/' . phpversion();
mail($to, $email_subject,$message,$email_body,$headers);
header("Content-type: text/html; charset=UTF-8");
header("Location: index.php");
//https://stackoverflow.com/questions/30802674/retrieve-data-from-cart-and-send-using-mail
?>
How can you pass dynamic details which are retrieved from a database (e.g. details in a shopping cart table) from one page to another page so that you could send them via email using the mail() function?I tried many ways using the "$message" but none worked. I am new to PHP and do not have much experience with it yet. Any help would be appreciated, thank you.
page 1:
<?php session_start();
//starting the session
include("adminarea/includes/DBcon.php");
include ("functions/php1.php");
include ("header.php");
require 'obj.php';
?>
<link rel="stylesheet" href = "styles/styling.css" media="all" />
<body>
<?php
//Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
$res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
$prod = mysqli_fetch_object($res);
$obj = new obj();
$obj->pID = $prod->pID;
$obj->pName = $prod->pName;
$obj->pPrice = $prod->pPrice;
$obj->qty = 1;
//to check if products exists in cart or not
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0;$i<count($cart);$i++)
if($cart[$i]->pID==$_GET['pID'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $obj;
else{
$cart[$index]->qty++;
$_SESSION['cart']=$cart;
}
echo "
<script>
window.open('cart.php','_self')
</script>
";
$_SESSION['pID'] = $_POST['pID'];
$_SESSION['pName'] = $_POST['pName'];
$_SESSION['pPrice'] = $_POST['pPrice'];
$_SESSION['qty'] = $_POST['qty'];
}
if(!(isset($_SESSION['cart']))){
echo "
<script>
alert('Shopping cart is empty!')
window.location.href='index.php';
</script>
";
}
//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
$cart = unserialize(serialize($_SESSION['cart']));
unset ($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<!-- This is to display the shopping cart table-->
<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
<td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2>
<tr>
<th style="color:#FFF">Option</th>
<th style="color:#FFF">Id</th>
<th style="color:#FFF">Name</th>
<th style="color:#FFF">Price</th>
<th style="color:#FFF">Quantity</th>
<th style="color:#FFF">SubTotal</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
$s += $cart[$i] ->pPrice * $cart[$i]->qty;
?>
<tr>
<td>
<div class="shopcart">
<input id="input" type="submit" name="ctable"/>Remove</input></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
</tr>
<?php }
$index++;
?>
<tr>
<td colspan="5" align="right" style="color:#FFF">Total</td>
<td style="color:#FFF" align="center">€<?php echo $s;?></td>
</tr>
</table>
<br>
<a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>
<div id="checkout">
<form id="checkout" method="post" action="checkout.php">
<input id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">
</div>
</div>
<?php include("footer.php") ?>
</body>
</html>
page 2:
<?php session_start();
require 'obj.php';
include("adminarea/includes/DBcon.php");
$to = "techologyy#gmail.com";//direction
$subject = "Purchase Details:";
$message = $_SESSION['pID'];
$message .= $_SESSION['pName']."\r\n";
$message .= $_SESSION['pPrice']."\r\n";
$message .= $_SESSION['qty']."\r\n";
$headers = 'From: techologyy#gmail.com' . "\r\n"; //from
//mail paramter with correct order
mail($to, $subject, $message, $headers);
//echo to display alert
echo "
<script>
alert('The checkout has been done successfully! Thank you')
window.location.href='index.php';
</script>
"; //returns the user back to homepage
?>
Please specify what you have tried so far.
you want to send the data from one page to another, try using form tag in html
and set the method attribute to post and action attribute to where you want to send the data.
The html code will look like this
<form action="post" action="getdata.php"> <input id="val" type="text" value="" name="cart" style="display:none;"> <button type="submit" >hit me</button> </form>
You can set the value of input using javascript
document.getElementById("val").value="YourValue";
getdata.php will look like this
> if ($_SERVER["REQUEST_METHOD"] == "POST"){
> $cartval=$_POST['cart'];
> echo $cartval; }
be sure to validate the data and check for any hidden code before executing the user input
So I have been working on a website and I tried to make a functional shopping cart with a checkout system. However, the shopping cart does not allow removal of added items in Mozilla Firefox, on other browsers it works well; I do not know why this is so.
Furthermore, my second problem is that in the checkout.php function, whenever I press the checkout button in the shopping cart page, it does not send the email of order to the business email. Is there a way how I can make this work please? Below I have put all the necessary code. Thank you.
cart.php:
<link rel="stylesheet" href = "styles/styling.css" media="all" />
<?php
session_start();
include("adminarea/includes/DBcon.php");
include ("functions/php1.php");
include ("header.php");
require 'obj.php';
?>
<body>
<?php
//Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
$res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
$prod = mysqli_fetch_object($res);
$obj = new obj();
$obj->pID = $prod->pID;
$obj->pName = $prod->pName;
$obj->pPrice = $prod->pPrice;
$obj->qty = 1;
//to check if products exists in cart or not
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0;$i<count($cart);$i++)
if($cart[$i]->pID==$_GET['pID'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $obj;
else{
$cart[$index]->qty++;
$_SESSION['cart']=$cart;
}
echo "
<script>
window.open('cart.php','_self')
</script>
";
}
if(!(isset($_SESSION['cart']))){
echo "
<script>
alert('Shopping cart is empty!')
</script>
";
echo "
<script>
window.open('products.php','_self')
</script>
";
}
//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
$cart = unserialize(serialize($_SESSION['cart']));
unset ($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<!-- This is to display the shopping cart table-->
<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
<td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2>
<tr>
<th style="color:#FFF">Option</th>
<th style="color:#FFF">Id</th>
<th style="color:#FFF">Name</th>
<th style="color:#FFF">Price</th>
<th style="color:#FFF">Quantity</th>
<th style="color:#FFF">SubTotal</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
$s += $cart[$i] ->pPrice * $cart[$i]->qty;
?>
<tr>
<td>
<div class="shopcart">
<button style="width:150px; height:50px;">Remove</button></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
<td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
<td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
</tr>
<?php }
$index++;
?>
<tr>
<td colspan="5" align="right" style="color:#FFF">Total</td>
<td style="color:#FFF" align="center">€<?php echo $s;?></td>
</tr>
</table>
<br>
<a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>
<div id="checkout">
<form id="checkout" method="post" action="checkout.php">
<input id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">
</div>
</div>
<?php include("footer.php") ?>
</body>
</html>
checkout.php:
<?php
//starting the session
session_start();
$from = "techologyy#gmail.com"; //from
$feedback = "Purchase details";
$to = "techologyy#gmail.com";//direction
$email = "Email from: $from \r\n";
mail("techologyy#gmail.com", $feedback, $from);
header("Location: index.php"); //returns the user back to homepage
echo "
<script>
alert('The checkout has been done successfully! Thank you')
</script>
";
?>
It looks to me like your mail() call is incorrect
The order of the parameters should be: to address, subject, email body and then sender. The sender should be in the format From: sender#email.com
So, adjust your code to something like this:
$from = "techologyy#gmail.com"; //from
$feedback = "Purchase details";
$to = "techologyy#gmail.com";//direction
$email = "Email from: $from \r\n";
mail($to, $feedback, $email, "From: $from");
I'm working in PHP and I have a simple shopping cart which adds items when you click the add button.
Everything is collected in a variable called $cartOutput
When I echo it, it gives me everything in the cart as expected. Same with var_dump . Everything is there
However, when I try to put it in an email and send it off. It cuts off the first item. Can anyone think of why this might be?
Nothing filters it before it is put into the email. It is simply what is in the variable
here is an example...
// e.g of the php variable being assembled for each item
$cartOutput .= "<tr>";
$cartOutput .= "<td>" . $product_name . "</td>";
$cartOutput .= "<td>$" . $price . "</td>";
// emailing the variables off here
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$company = $_POST['company'];
$name = $_POST['name'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$commercialAdd = $_POST['commercial'];
$residentialAdd = $_POST['residential'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal_code = $_POST['postal_code'];
$email = $_POST['email'];
$special_instructions = $_POST['special_instructions'];
$date = date("Y/m/d");
$time = date("h:i:sa");
$to = "xxx#gmail.com";
$header = "Cc:xxx#somedomain.com \r\n";
$subject = "Email Order - $company ($date - $time)";
$message = <<<EOD
<h1>Email Order - $date - $time </h1>
<h3><strong><u>Company:</u></strong> $company</h3>
<h3><strong><u>Name:</u></strong> $Name </h3>
<h3><strong><u>Address:</u></strong> $address<br>
$address2</h3>
<h3><strong><u>Residential:</u></strong> $commercialAdd </h3>
<h3><strong><u>Commercial:</u></strong> $residentialAdd </h3>
<h3><strong><u>City:</u></strong> $city</h3>
<h3><strong><u>Province:</u></strong> $province</h3>
<h3><strong><u>Postal Code:</u></strong> $postal_code</h3>
<h3><strong><u>Phone Number:</u></strong> $phone</h3>
<h3><strong><u>Email:</u></strong> $email</h3>
<h3><strong><u>Special Instructions:</u></strong> $special_instructions</h3>
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Weight (Kg)</th>
<th>Qty</th>
<th>Subtotal</th>
<th></th>
</tr>
</thead>
<tbody>
$cartOutput
<tr>
<td class="totals"><strong>Total</strong></td>
<td class="totals"> </td>
<td class="totals">$weightTotal kg</td>
<td class="totals">$quantityTotal</td>
<td class="totals">$ $cartTotal</td>
<td class="totals"> </td>
<tr>
</tbody>
</table>
EOD;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
header("location: complete.php");
exit();
}
else {
echo "Order could not be sent. Please try again or contact our office for assistance";
}
}
Probably you are running into an email format issue. mail() requires the body to end each line in \r\n AND be less than 70 characters each. You will need to encode you HTML or include it as an attachment. See this set of instructions for an example.