douhuang1973 2019-05-25 23:51
浏览 223
已采纳

我有一个PhP购物车,我需要捕获所有选择的产品,并在点击“提交”后将其与联系表单一起发送到我的电子邮箱

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' . "
";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

        mail($to, $subject, $txt, $headers);
        echo '<div style="font-weight:bold;font-size:20px;text-align:center">Thank you for using our services.</div>'   ;
    }
}


?>
  • 写回答

1条回答 默认 最新

  • douguabu8960 2019-05-26 00:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向