2011-12-23 57 views
0

我正在建立一個小購物車。目前,我正在嘗試實施檢查每個產品庫存的結賬功能,然後再繼續進行付款頁面。通過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.任何想法爲什麼錯誤的值被讀取和傳遞到數組中?或者,也許我在上述方法中犯了錯誤?

+1

您可以顯示生成的HTML而不是生成它的PHP嗎? – lonesomeday 2011-12-23 09:21:37

+0

你是什麼意思這個按鈕不會產生任何html只是一個錯誤消息,如果qty <股票。 – 2011-12-23 09:29:06

+0

生成HTML表格的PHP。 HTML會更有用。 ! – lonesomeday 2011-12-23 09:56:50

回答

1

只需你可以做到這一點

data: { productId : productId }, 

that's it! now you can access it in PHP: 

    <? $myArray = $_REQUEST['productId ']; ?> 

var myJSONText = JSON.stringify(myObject); 

所以

['Location Zero', 'Location One', 'Location Two']; 

將變爲:

"['Location Zero', 'Location One', 'Location Two']" 

數據可以以類似的方式從服務器返回。即你可以把它變回一個對象。

var myObject = JSON.parse(myJSONString); 

看到這個問題

Passing JavaScript array to PHP through jQuery $.ajax

3

這是不正確的:

productId[j]==$(this).val(); 

你做一個平等檢查==,而不是分配=

更改爲:

productId[j]=$(this).val(); 
+0

感謝現在即時通訊一步向前來使這項工作....正在公佈的參數是: '的productId [] 的productId [] \t 1' 和源顯示了這種'的productId%5B% 5D = 2&的productId%5B%5D = 1' – 2011-12-23 10:00:22

1

Andreas表明,你需要做一個分配=而不是==測試是否相等。這是正確的,我認爲你的代碼不起作用的原因。然而,有,一個更好的方式來構建整個代碼段,使用jQuery的map功能:

$('#btnCheckout').click(function() { 
    var productId = $('.ProductId').map(function() { 
     return this.value; 
    }).get(); 

    $.ajax({ 
     type: "POST", 
     url: "functions/checkProductStock.php", 
     data: { 
      productId: productId 
     }, 
     success: function (msg) { 
      alert(msg); 
     } 
    }); 
}); 

請注意,我也用同樣的技術Kanishka,這樣你就可以訪問值$_POST['productId']