我有一个PhP购物车,我需要捕获所有选择的产品,然后单击“提交”,将它们连同联系表格一起发送到我的电子邮件中

Seekerz

我有一个用于汽车零部件的PhP购物车,我需要捕获所有选择的产品,并在单击“提交订单”后将它们连同联系表格一起发送到我的电子邮件中。到目前为止,我仅发送了第一个选定的产品,但是我需要捕获所有选定的产品,而不仅仅是一个。

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

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 = '[email protected]';

         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来发送电子邮件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章