2015-07-01 52 views
6

我有一些問題。首先,我想將我的數據存儲到數組集合中。然後將數據傳遞給控制器​​提交。這裏是我的代碼從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 
    ) 

) 

但是,在我的控制器結果並未如預期。

回答

6

collection變量是本地環路,並且不保存您遍歷數據。相反,嘗試這樣的事情,雖然你並不真的需要一個對象來停止該指數src - 一個普通的一維數組會做:

$("#submit").click(function() { 
    var total = 3; 
    var photos = new Array(); 
    for(var i = 0; i < total; i++) 
    { 
     var collection = { 
      'no' : i, 
      'photo' : $('#thumbnail'+i+'').children('img').attr('src') 
     }; 
     photos.push(collection); 
    } 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo base_url()?>create/submit", 
     data: {collection : photos}, 
     cache: false, 
     success: function(response) 
     { 
      console.log(response); 
      alert('success'); 
      window.location = '<?php echo base_url()?>create/submit'; 
     } 
    }); 
}); 

您發送的數據的格式爲:

photos = [ 
    { 
     "no": 1, 
     "photo":"this is a link" 
    }, 
    { 
     "no": 2, 
     "photo":"this is a link" 
    }, 
    { 
     "no": 3, 
     "photo":"this is a link" 
    } 
] 
+0

謝謝PeterKa!我認爲它有效。但我仍然有問題從ajax獲取數據到控制器並顯示它(在控制器中)。我必須做什麼? – dionajie

+1

在您的控制器輸出中有什麼'print_r($ collection);'? – PeterKA

+0

沒有什麼:( – dionajie

4

您已添加thumbnail作爲所有<a>的類別。 然後改變這樣的代碼:

$("#submit").click(function() { 
     var total = 3; 
     var photos = new Array(); 
     $('.thumbnail').each(function (i) { 
      photos.push($(this).attr("src")); 
     }); 
     $.ajax({ 
      type: "POST", 
      url: "<?php echo base_url() ?>create/submit", 
      data: {'collection': photos}, 
      cache: false, 
      success: function (response) 
      { 
       console.log(response); 
       alert('success'); 
       window.location = '<?php echo base_url() ?>create/submit'; 

      } 
     }); 

    }); 
+0

我不明白你的建議,你能解釋一下嗎? –

+0

javascript'for' = jquery'each' –

相關問題