Question

I am making a shopping cart and I am working on the check out page. Right now I am trying to create a PHP page that will gather all the products that are in the user's cart, then email me the products that are in the user's cart. The email should include the price, product name, quantity, product description, and the total price of all the products, underneath. So far, the page is successfully displaying the user's products in the shopping cart, instead of sending them to me in an email. I am unsure how to do this, as I would have to somehow gather the whole foreach code and put it into a variable.

Here is my full PHP page code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
if (isset($_POST['email'])) {
    session_start();
        include_once("config.php");
        if(isset($_SESSION["products"]))
     {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
            $cart_items ++;

        }
        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
        ?>
             <?php
    }else{
        echo 'Your Cart is empty';
    }   
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$cname= $_POST['cname'];
$email= $_POST['email'];
$phone= $_POST['phone'];
$fax= $_POST['fax'];
$address= $_POST['address'];
$city= $_POST['city'];
$province= $_POST['province'];
$postal= $_POST['postal'];

$date = gmdate("M d Y");

print"<p>Thank you, $fname! we will get back to you.</p>";
print"<p>Today's date is $date.</p>";

$to = "myemail@gmail.com";
$subject = "Parts Order for $fname $lname";
$body = " Date: $date \n\n Note: If any fields have been left blank it means the user did not input anything. \n\n First name: $fname \n Last name: $lname \n Company: $cname \n Email: $email \n Phone: $phone \n Fax: $fax \n Address: $address \n City: $city \n Province: $province \n Postal Code: $postal \n\n";
$headers = "From: info@gbmtrailer.ca";
mail($to, $subject, $body, $headers);
} else {
    echo '<script>alert("Please enter valid data before submitting! Form was not sent.");</script>';
}
?>
</body>
</html>

Thank you for any help. All help is greatly appreciated.

Was it helpful?

Solution

you can try with the functions ob_start();, this function sends all the data where is not in the

OTHER TIPS

Why not do it as simple as assign all the value to another variable and add to body later on. Something like...

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php

$bodyAdd=""; //CREATE EMPTY VAR @@@@@@@@@@@@@@@@@@@@@@@@

if (isset($_POST['email'])) {
    session_start();
        include_once("config.php");
        if(isset($_SESSION["products"]))
     {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
            $cart_items ++;
//While running foreach add selected values to your variable @@@@@@@@@@@@@@
            $bodyAdd .="\r\n".$cart_items.") Product name: ".$obj->product_name . " Product qty: " . $obj->product_name; //AND SO SO ON @@@@@@@

        }
        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
        ?>
             <?php
    }else{
        echo 'Your Cart is empty';
    }   
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$cname= $_POST['cname'];
$email= $_POST['email'];
$phone= $_POST['phone'];
$fax= $_POST['fax'];
$address= $_POST['address'];
$city= $_POST['city'];
$province= $_POST['province'];
$postal= $_POST['postal'];

$date = gmdate("M d Y");

print"<p>Thank you, $fname! we will get back to you.</p>";
print"<p>Today's date is $date.</p>";

$to = "myemail@gmail.com";
$subject = "Parts Order for $fname $lname";
$body = " Date: $date \n\n Note: If any fields have been left blank it means the user did not input anything. \n\n First name: $fname \n Last name: $lname \n Company: $cname \n Email: $email \n Phone: $phone \n Fax: $fax \n Address: $address \n City: $city \n Province: $province \n Postal Code: $postal \n\n";

$body .= $bodyAdd; //JOIN CONTAINING VAR TO MESSAGE BODY @@@@@@@@@@@@@@

$headers = "From: info@gbmtrailer.ca";
mail($to, $subject, $body, $headers);
} else {
    echo '<script>alert("Please enter valid data before submitting! Form was not sent.");</script>';
}
?>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top