通过Ajax向控制器传递数组的数组

6

我有一些问题。首先,我想将我的数据存储到数组集合中。然后将数据传递给控制器提交。这是我的代码:

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
    )

)

但是,我的控制器中的结果并不如预期那样。
2个回答

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"
    }
]

谢谢PeterKa!我认为它有效。但是我仍然有问题,无法从ajax获取数据并在控制器中显示它。我该怎么做? - dionajie
1
你的控制器中的 print_r($collection); 输出了什么内容? - PeterKA
1
尝试使用print_r($_POST); - Ashok Maharjan
我的问题是结果与预期不符。集合没有被识别为数组。 - dionajie
在我看来,这似乎是一个有效的数组。你使用的 PHP 版本是什么?我在这里测试过了,它被识别为一个数组。如果你用 foreach 处理它会发生什么? - PeterKA
我只是试图用另一个字符串(不是img scr)替换照片,而且它起作用了!我想是因为照片包含URL。但我不知道为什么。谢谢PeterKa! - dionajie

4
你需要给所有的 <a> 添加一个 thumbnail 类。然后像下面这样改变代码:
$("#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';

            }
        });

    });

我不理解你在建议什么,可以再解释一下吗? - Viraj Nalawade
JavaScript中的for等同于jQuery中的each - Nithin Krishnan P

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接