简体   繁体   English

通过 php 邮件 -ajax 发送购物车详细信息

[英]sending the cart details through php mail -ajax

Im working on a JS shopping cart site and I am trying to send the cart details to mail at the check out function using php mail,here im passing my cart details to php via ajax. Im working on a JS shopping cart site and I am trying to send the cart details to mail at the check out function using php mail,here im passing my cart details to php via ajax.

in php when try to send all the cart values using foreach im only able to recive the just last row of cart as foreach is repalcing the previous value在 php 中,当尝试使用 foreach 发送所有购物车值时,我只能接收最后一行购物车,因为 foreach 正在替换先前的值

how do i retrive the cart values and send them in a format我如何检索购物车值并以某种格式发送它们

js js

function SendMail() {
    var tableContent = localStorage.getItem('productsInCart');
    $.post('read.php', {tableContent: tableContent}, function (data) {
        console.log(tableContent);
    });
}

php php

if (isset($_POST['tableContent'])) {
    $tableContent = json_decode($_POST['tableContent']);
    foreach ($tableContent as $tableContent) {
        $name = ($tableContent->name);
        $price = ($tableContent->price);
        $quantity = ($tableContent->inCart);
    }

    $mailTo = "xxxxxxxxxxxxx";
    $Subject = " order details ";
    $headers = "from :" . $contact;
    $txt = "New registration \n Item:" . $name . "\n Quantity:" . $quantity . "\n Price:" . $price . "\n\n\n CUSTOMER DERAILS\n\n Name:" . $contact . "\n Reg No:" . $reg;

    mail($mailTo, $Subject, $txt, $headers);
    header("location: read.php?mailsend");
}

You're currently overwriting the same variables on each iteration of the loop, which is why they will only contain the last entry.您当前在循环的每次迭代中都覆盖了相同的变量,这就是它们只包含最后一个条目的原因。

You should append the values instead, doing something like:您应该改为 append 值,执行以下操作:

$tableContent = json_decode($_POST['tableContent']);

// Define a variable to store the items in
$items = '';
// Let's add a total sum as well
$total = 0;

// Let's also use different variable names here
foreach ($tableContent as $item) {        
    // Append to the variable (notice the . before the =)
    $items .= 'Item: ' . $item->name . "\n";
    $items .= 'Quantity: ' . $item->inCart . "\n";
    $items .= 'Price: ' . $item->price . "\n\n";
    
    // Add the price to the total (I'm assuming that the price is an integer)
    $total += $tableContent->price;
}

Now when outputting the email body, we have all the items and the total in those variables:现在,当输出 email 主体时,我们在这些变量中有所有项目和总数:

$txt = "New registration \n" . $items . "Sum total: " . $total . "\n\n\n CUSTOMER DERAILS\n\n Name:".$contact."\n Reg No:".$reg;

As you can see, I changed the layout of the mail a bit since the cart seems to be able to contain several items while your email body was written as if only could contain one.正如您所看到的,我稍微更改了邮件的布局,因为购物车似乎能够包含多个项目,而您的 email 正文被写入好像只能包含一个。

A warning about this approach关于这种方法的警告

You shouldn't get the cart values, like name and price, from the client in a POST request like this.您不应该在这样的 POST 请求中从客户端获取购物车值,例如名称和价格。 The client should only send the item id and quantity and then you would fetch the name and price from a database or similar in the backend.客户端应该只发送商品 ID 和数量,然后您将从数据库或后端的类似数据库中获取名称和价格。 Otherwise, anyone can modify the price to be anything they want before it's posted.否则,任何人都可以在发布之前将价格修改为他们想要的任何东西。 Never ever trust user data.永远不要相信用户数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM