我有一个 PhP 购物车,我需要捕获所有选择的产品并在单击 "Submit" 后将它们与联系表格一起发送到我的电子邮件

I have a PhP shopping cart and I need to capture all the chosen products and send them to my email together with a contact form upon clicking "Submit"

我有一个 PhP 汽车配件购物车,我需要捕获所有选择的产品,并在单击 "Submit order" 后将它们连同联系表格一起发送到我的电子邮件中。到目前为止,我已经成功发送了第一个选择的产品,但我需要捕获所有选择的产品,而不仅仅是一个。

我尝试使用所选产品的所有属性捕获数组本身,但它只向我发送第一个所选产品。

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() 函数

<?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>'   ;
    }
}


?>

这是因为您应该连接字符串 $txt,因此请使用 .= 而不是 =

当时您只发送 $_SESSION["cart_item"] 中的最后一项(不是第一项)

您的情况:

 $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>';

     };
     }

顺便说一句,我强烈推荐使用phpmailer (https://github.com/PHPMailer/PHPMailer) 或swiftmail 发送邮件。