2011-02-28 86 views
0

我確定我犯了一個簡單的錯誤......但根本找不到它。我最終從一個Android應用程序(該部分正在工作)發佈JSON數組,但暫時我只是在兩個PHP頁面之間進行測試(1:測試PHP頁面與基本窗體,以及2:CodeIgniter最終目的地)這是我有:JSON_decode數組發佈到CodeIgniter的問題

在表單頁面:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post"> 
    <?php 
     $array = array("items"=>array(
      "taxable"=>1, 
      "quantity"=>1, 
      "amount"=>123.99, 
      "work_description"=>"this is a test")); 
     $json = json_encode($array); 
    ?> 
    <input type="hidden" name=json value=<?php $json ?> /> 
    <input type="submit" name="btnSendForm" value="Send" /> 
</form> 

這將創建(這對我來說很好):

{"items":{"taxable":1,"Quantity":1,"amount":123.99,"work_description":"this is a test"}} 

在笨SID E,I有:

$input = $this->input->post('json'); 
$items = json_decode($input, TRUE); 

$amount = 0; 
foreach ($items as $item) // In case there are multiple 'items' 
{ 
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0; 

    $invoice_items = array(
     'quantity' => $item['quantity'], 
     'amount' => $item['amount'], 
     'work_description' => $item['work_description'], 
     'taxable' => $taxable 
    ); 

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB 
} 

最後,我收到錯誤:(我在我所有的調整實際上是獲得了無數的錯誤,但是這是一個我似乎無法撼動)

Message: Invalid argument supplied for foreach() 

已編輯 - 糾正錯字。

+0

您是否嘗試過'var_dump()'ing'$ items'和'$ json'來確保它們符合您的期望? – Shad 2011-02-28 22:50:37

+0

您正在將未轉義的'JSON'數據轉儲到屬性。我假設這在DOM中造成了一些破壞,並且POST數據也被破壞了。 – Dan 2011-02-28 22:51:12

+0

如果在這裏不是拼寫錯誤,那麼這一行' />'對我來說看起來不對。你沒有迴應數據。它應該是''或'<?php echo $ json?>' – 2011-02-28 22:53:27

回答

3

當您的表單發佈名爲json的隱藏值時,您正在使用$this->input->post('items')

如果您var_dump($this->input->post('items')),它應該是FALSENULL

在CI腳本試試這個:

$input = $this->input->post('json'); // not 'items' 
$items = json_decode($input, TRUE); 

// Rest of your code... 

這應該解決這個問題,但你還需要確保你的JSON數據被髮送到正確的開始! var_dump($_POST)應該告訴你它是否將它製作成一個腳本。

+0

Aaaaaarrghh !!! (嘆氣......)它正在工作,就像它應該。謝謝bschaeffer,我不能告訴你這個愚蠢的錯誤讓我感到惱火。 – ctgScott 2011-03-01 05:25:13

+0

是啊......那些事情很難注意,因爲你從來沒有想過你把這個「容易的部分」搞砸了。樂意效勞! – bschaeffer 2011-03-02 02:29:29

0

試試這個
<input type="hidden" name=json value='<?=$json?>' /> 或者這
<input type="hidden" name=json value='<?=str_replace('\'', '&#039;', $json)?>' />
htmlspecialchars

+0

謝謝,azat。你是對的...我需要糾正這條線。現在是: /> – ctgScott 2011-02-28 23:44:46

0

當你編碼協會。數組作爲JSON它變成一個對象,不再是一個數組。現在,當你解碼你的JSON php創建你的JSON對象而不是一個assoc。陣列。

+0

沒有正確..看到這個http://ru2.php.net/manual/en/function.json-decode.php,第二個參數 – azat 2011-02-28 22:54:36

+0

對不起,我的錯誤。 – icc 2011-02-28 22:58:53

0

你可以改變和不使用json_encode並在你的數組中使用serialize。

這樣在您的形式:

<form action="bambooinvoice/index.php/api2/newinvoice/4/0/0" method="post"> 
    <?php 
     $array = array("items"=>array(
      "taxable"=>1, 
      "quantity"=>1, 
      "amount"=>123.99, 
      "work_description"=>"this is a test")); 
     $json = serialize($array); 
    ?> 
    <input type="hidden" name=json value="<?php echo $json ?>" /> 
    <input type="submit" name="btnSendForm" value="Send" /> 
</form> 

此序列化功能將創建此輸出:

a:1:{s:5:"items";a:4:{s:7:"taxable";i:1;s:8:"quantity";i:1;s:6:"amount";d:123.9899999999999948840923025272786617279052734375;s:16:"work_description";s:14:"this is a test";}} 

然後在笨方面,你可以這樣做:

<?php 
$input = $this->input->post('json'); 
$items = unserialize($input); 

$amount = 0; 
foreach ($items as $item) // In case there are multiple 'items' 
{ 
    $taxable = (isset($item['taxable']) && $item['taxable'] == 1) ? 1 : 0; 

    $invoice_items = array(
     'quantity' => $item['quantity'], 
     'amount' => $item['amount'], 
     'work_description' => $item['work_description'], 
     'taxable' => $taxable 
    ); 

    $this->_addInvoiceItem($invoice_items); //simply adding contents to DB 
} 
?> 

但要小心,因爲當你使用一個包含更多1級的數組的foreach時。 我希望這對你有所幫助。