我正在建立一個小購物車。目前,我正在嘗試實施檢查每個產品庫存的結賬功能,然後再繼續進行付款頁面。通過jQuery數組與php與json
這while循環從「購物車」表中顯示的每一行:
while ($row = $result->fetch()) {
echo '<table class="cart-row" cellspacing="0" cellpadding="0" width="100%">';
echo '<tbody>';
echo '<tr>';
echo '<td width="75"><img border="0" width="59px" height="78px" title="" alt="" src=' . $row["ImagePath"] .'></td>';
echo '<td width="203"><span class="itemElements itemName">'. $row["Name"] .'</span></td>';
echo '<td width="203"><input type="submit" class="btnMinus linkbtnType2" value="-"><input type="submit" class="btnPlus linkbtnType2" value="+"></td>';
echo '<td width="135"><span class="qtyNum">('. $row["Qty"] .')</span> <br />';
echo '<input type="hidden" name="ProductId" class="ProductId" value='.$row["ProductId"].' />';
echo '<span class="qtyRemoveLink"><input type="submit" class="btnRemove linkbtn" value="Remove"></td>';
echo '<td width="180"><span class="itemElements orderStatus">In Stock Usually dispatched within 24 hours</span></td>';
echo '<td width="175" class="itemPriceRow"><span id="itemPrice">€ '. $row["Price"]* $row["Qty"] .'</span></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<br>';
}
在該while循環的隱藏輸入按鈕的值被加載有對應於產品ID返回的行。
現在我有了這個jQuery方法,它應該讀取頁面中的所有procuctId值並將它們存儲在數組中。當我使用螢火傳遞給該方法的唯一值是productId=%5B%5D
<?php include "../config.php" ?>
<?php
$productIds = json_decode($_POST['productId']);
var_dump($productIds);
//foreach loop that iterate through an array containing the product IDs and exexcutes the below method.
foreach ($productIds as $productId)
{
$CartDAL = new CartDAL();
$result = $CartDAL->get_ProductInCartById($productId, $_SESSION["username"]);
$result = $ProductsDAL->get_ProductById($productId);
$rowProduct = $result->fetch();
//Get Product Quatity Form Cart
$qty = $row["Qty"];
//Get Product Stock
$stock = $rowProduct["AvailableStock"];
if($qty <= $stock)
{
$CartDAL->remove_ProductInCart($productId, $_SESSION["username"]);
}
else
{
echo $rowProduct["Name"].' is out of stock!';
}
}
?>
不知何故:
$('#btnCheckout').click(function() {
var productId = new Array();
var j = 0;
$(".ProductId").each(function() {
productId[j] == $(this).val();
j++;
});
$.ajax({
type: "POST",
url: "functions/checkProductStock.php",
data: {
productId: JSON.stringify(productId)
},
success: function (msg) {
alert(msg);
}
})
})
該方法然後轉到這些值的PHP方法。 FYI應該傳遞的值是1和2.任何想法爲什麼錯誤的值被讀取和傳遞到數組中?或者,也許我在上述方法中犯了錯誤?
您可以顯示生成的HTML而不是生成它的PHP嗎? – lonesomeday 2011-12-23 09:21:37
你是什麼意思這個按鈕不會產生任何html只是一個錯誤消息,如果qty <股票。 – 2011-12-23 09:29:06
生成HTML表格的PHP。 HTML會更有用。 ! – lonesomeday 2011-12-23 09:56:50