title: pass jquery array to php with json
body: <p>I'm building a small shopping cart. Currently I'm trying to implement the checkout feature which checks each product's stock before proceeding to the payment page.</p>
  
  <p>This while loop is displaying each row from the "shoppingcart" table:</p>
  
  <pre><code>while ($row = $result-&gt;fetch()) {
       echo '&lt;table class="cart-row" cellspacing="0" cellpadding="0" width="100%"&gt;';
       echo '&lt;tbody&gt;';
       echo '&lt;tr&gt;';
       echo '&lt;td width="75"&gt;&lt;img border="0" width="59px" height="78px" title="" alt="" src=' . $row["ImagePath"] .'&gt;&lt;/td&gt;';
       echo '&lt;td width="203"&gt;&lt;span class="itemElements itemName"&gt;'. $row["Name"] .'&lt;/span&gt;&lt;/td&gt;';
       echo '&lt;td width="203"&gt;&lt;input type="submit" class="btnMinus linkbtnType2" value="-"&gt;&lt;input type="submit" class="btnPlus linkbtnType2" value="+"&gt;&lt;/td&gt;';
       echo '&lt;td width="135"&gt;&lt;span class="qtyNum"&gt;('. $row["Qty"] .')&lt;/span&gt; &lt;br /&gt;';
       echo '&lt;input type="hidden" name="ProductId" class="ProductId" value='.$row["ProductId"].' /&gt;';
       echo '&lt;span class="qtyRemoveLink"&gt;&lt;input type="submit" class="btnRemove linkbtn" value="Remove"&gt;&lt;/td&gt;';                            
       echo '&lt;td width="180"&gt;&lt;span class="itemElements orderStatus"&gt;In Stock Usually dispatched within 24 hours&lt;/span&gt;&lt;/td&gt;';
                                      echo '&lt;td width="175" class="itemPriceRow"&gt;&lt;span id="itemPrice"&gt;€ '. $row["Price"]* $row["Qty"]  .'&lt;/span&gt;&lt;/td&gt;';
        echo '&lt;/tr&gt;';
        echo '&lt;/tbody&gt;';
        echo '&lt;/table&gt;';
        echo '&lt;br&gt;';                            
  }
  </code></pre>
  
  <p>In that while loop a hidden input button's value is being loaded with the productId corresponding to that returned row.</p>
  
  <p>Now I have this jQuery method which is supposed to read all the procuctId values in the page and store them in an array.</p>
  
  <pre><code>$('#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);
          }
      })
  })
  </code></pre>
  
  <p>This method then passes those values to a PHP method:</p>
  
  <pre><code>&lt;?php include "../config.php" ?&gt;
  &lt;?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-&gt;get_ProductInCartById($productId, $_SESSION["username"]);   
  
          $result = $ProductsDAL-&gt;get_ProductById($productId);
          $rowProduct = $result-&gt;fetch();
  
            //Get Product Quatity Form Cart
            $qty = $row["Qty"];
  
            //Get Product Stock
            $stock = $rowProduct["AvailableStock"];   
  
            if($qty &lt;= $stock)
            {
                $CartDAL-&gt;remove_ProductInCart($productId, $_SESSION["username"]);     
            }
            else
            {
                echo $rowProduct["Name"].' is out of stock!';
            }  
      }
  ?&gt;
  </code></pre>
  
  <p>Somehow when I'm using firebug the only value that are passed to this method are <code>productId=%5B%5D</code>. FYI the values that are supposed to be passed are 1 and 2. Any ideas why the wrong values are being read and passed in the array? Or maybe I made a mistake in the above methods?</p>