1 回答

TA貢獻1735條經驗 獲得超5個贊
您目前正在循環的每次迭代中覆蓋相同的變量,這就是為什么它們只包含最后一個條目的原因。
您應該改為附加值,執行以下操作:
$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;
}
現在在輸出電子郵件正文時,我們在這些變量中擁有所有項目和總數:
$txt = "New registration \n" . $items . "Sum total: " . $total . "\n\n\n CUSTOMER DERAILS\n\n Name:".$contact."\n Reg No:".$reg;
如您所見,我稍微更改了郵件的布局,因為購物車似乎可以包含多個項目,而您的電子郵件正文寫得好像只能包含一個。
關于這種方法的警告
您不應該在這樣的 POST 請求中從客戶端獲取購物車值,例如名稱和價格??蛻魬撝话l送商品 ID 和數量,然后您將從后端的數據庫或類似數據庫中獲取名稱和價格。否則,任何人都可以在發布之前將價格修改為他們想要的任何值。永遠不要相信用戶數據。
添加回答
舉報