问题描述
我确定我犯了一个简单的错误……但根本找不到它.
I am certain I am making a simple mistake... but simply can't find it.
最终我从一个Android应用程序发布了一个JSON数组(该部分正在工作),但是暂时我只是在两个PHP页面之间进行测试(1:使用基本形式测试PHP页面,而2:CodeIgniter final目的地)这是我所拥有的:
Ultimately I am posting a JSON array from an Android app (that part is working), but for the time being I am simply testing between two PHP pages (1: test PHP page with basic form, and 2: the CodeIgniter final destination) Here is what I have:
在表单页面上:
<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>
这创建了(对我来说看起来不错):
This creates (which looks good to me):
{"items":{"taxable":1,"Quantity":1,"amount":123.99,"work_description":"this is a test"}}
在codeIgniter方面,我有:
On the codeIgniter side, I have:
$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
}
最后我收到错误消息:(我在所有调整中实际上都收到了许多错误,但这是我似乎无法撼动的错误)
In the end I receive the error: (i have received numerous errors actually in all my tweaking, but this is the one I can't seem to shake)
Message: Invalid argument supplied for foreach()
已编辑-纠正错字.
推荐答案
当表单发布名为json
的隐藏值时,您正在使用$this->input->post('items')
.
You are using $this->input->post('items')
when your form is posting a hidden value named json
.
如果您var_dump($this->input->post('items'))
,则应为FALSE
或NULL
.
改为在您的CI脚本中尝试以下操作:
Try this in your CI script instead:
$input = $this->input->post('json'); // not 'items'
$items = json_decode($input, TRUE);
// Rest of your code...
这应该可以解决该问题,但是您还需要确保正确发送json数据! var_dump($_POST)
应该向您展示它是否一次完成了您的脚本.
That should fix that problem, but you also need to make sure your json data is being sent correctly to begin with! var_dump($_POST)
should show you if it's making it to your script in one piece.
这篇关于数组发布到CodeIgniter的JSON_decode问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!