【问题标题】:Passing Array of arrays to controller from Ajax将数组数组从 Ajax 传递给控制器
【发布时间】:2015-09-18 02:27:55
【问题描述】:

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

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
    )

)

但是,结果在我的控制器中并没有像预期的那样。

【问题讨论】:

    标签: javascript php jquery ajax codeigniter


    【解决方案1】:

    您已将thumbnail 添加为所有&lt;a&gt; 的类。 然后像这样更改代码:

    $("#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';
    
                }
            });
    
        });
    

    【讨论】:

    • 我不明白你的建议你能解释一下吗?
    • javascript for= jquery`each'
    【解决方案2】:

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

    【讨论】:

    • 谢谢彼得卡!我认为它有效。但是我仍然无法从 ajax 获取数据到控制器并显示它(在控制器中)。我该怎么办?
    • 控制器输出中的print_r($collection); 是什么?
    • 试试print_r($_POST);
    • 我现在的问题是结果不如预期。集合不检测为数组
    • 对我来说看起来像是一个有效的数组。你用的是什么版本的PHP?我测试了它here,它作为一个数组传递。如果您使用foreach 处理它会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2011-12-21
    • 2020-12-05
    相关资源
    最近更新 更多