如何通过Foreach和PHP向MSSQL表数据发送电子邮件

时间:2014-03-03 22:30:58

标签: php sql-server email foreach shopping-cart

我正在制作购物车,我正在检查结账页面。现在我正在尝试创建一个PHP页面,它将收集用户购物车中的所有产品,然后通过电子邮件发送给用户购物车中的产品。电子邮件应包括下面所有产品的价格,产品名称,数量,产品描述和总价。到目前为止,该页面已成功在购物车中显示用户的产品,而不是通过电子邮件将其发送给我。我不确定如何做到这一点,因为我必须以某种方式收集整个foreach代码并将其放入变量中。

这是我的完整PHP页面代码:

<!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>

感谢您的帮助。非常感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

你可以尝试使用函数ob_start();,这个函数发送不在的所有数据

答案 1 :(得分:0)

为什么不这样做就像将所有值分配给另一个变量并稍后添加到body中一样简单。 有点像...

<!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>