我確定我犯了一個簡單的錯誤......但根本找不到它。我最終從一個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()
已編輯 - 糾正錯字。
您是否嘗試過'var_dump()'ing'$ items'和'$ json'來確保它們符合您的期望? – Shad 2011-02-28 22:50:37
您正在將未轉義的'JSON'數據轉儲到屬性。我假設這在DOM中造成了一些破壞,並且POST數據也被破壞了。 – Dan 2011-02-28 22:51:12
如果在這裏不是拼寫錯誤,那麼這一行' />'對我來說看起來不對。你沒有迴應數據。它應該是'=$json?>'或'<?php echo $ json?>' – 2011-02-28 22:53:27