我有一些問題。首先,我想將我的數據存儲到數組集合中。然後將數據傳遞給控制器提交。這裏是我的代碼從Ajax向控制器傳遞陣列數組
Ajax.php
$("#submit").click(function() {
var total = 3;
var photos = new Array();
for(var i = 0; i < total; i++)
{
photos[i] = $('#thumbnail'+i+'').children('img').attr('src');
var collection = {
'no' : i,
'photo' : photos[i]
};
}
$.ajax({
type: "POST",
url: "<?php echo base_url()?>create/submit",
data: {collection : collection},
cache: false,
success: function(response)
{
console.log(response);
alert('success');
window.location = '<?php echo base_url()?>create/submit';
}
});
});
[編輯]
控制器
function submit()
$collection = $this->input->post('collection');
print_r($collection);
if(is_array($collection)) {
foreach ($collection as $collect) {
echo $collect['no'];
echo $collect['photo'];
}
}
else
{
echo 'collection is not array!';
}
}
結果
collection is not array!
基於PeterKa的解決方案,我得到這個我的控制檯在控制檯
Array
(
[0] => Array
(
[no] => 0
[photo] => https://scontent.cdninstagram.com/hphotos-xap1/t51.2885-15/s320x320/e15/11176494_1106697872689927_2104362222_n.jpg
)
[1] => Array
(
[no] => 1
[photo] => https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s320x320/e15/11376044_838742186174876_410162115_n.jpg
)
[2] => Array
(
[no] => 2
[photo] => https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e15/11381470_878168042272606_1132736221_n.jpg
)
)
但是,在我的控制器結果並未如預期。
謝謝PeterKa!我認爲它有效。但我仍然有問題從ajax獲取數據到控制器並顯示它(在控制器中)。我必須做什麼? – dionajie
在您的控制器輸出中有什麼'print_r($ collection);'? – PeterKA
沒有什麼:( – dionajie