【问题标题】:Can codeigniter receive post array?codeigniter 可以接收 post 数组吗?
【发布时间】:2012-01-31 04:20:00
【问题描述】:

我正在尝试使用 $this->input->post() 接收从视图页面发送的帖子数组。但是,似乎 CI 无法做到这一点。有什么想法吗?

控制器

public function pub()
    {
    // the postArray is an array: $postArray['t1']=test1, $postArray['t2']=test2 
    $go=$this->input->post('postArray');    
    foreach ($go as $test){

    echo $test['t1'];  //show nothing
    echo $test['t2'];  //show nothing

    }

//the following code would work if I sent the $postArray as a string variable

 public function pub()
    {
    // the postArray is an string variable $postArray='test1'
    $go=$this->input->post('postArray');    

    echo $go; //show test1

        }

感谢您的帮助。

更新: 以下是我查看页面中的Jquery代码

   //postArray is an array
     $.post('<?=base_url()?>/project_detail/pub', {'postArray':postArray},function(go)
        {
            alert(go);
        })

【问题讨论】:

    标签: arrays codeigniter post


    【解决方案1】:

    您检查过您的 HTML 吗?您应该将[] 合成包含到name 属性中以创建数组,例如

    <input type="text" value="something..." name="postArray[]" />
    <input type="text" value="something..." name="postArray[]" />
    

    print_r($this-&gt;input-&gt;post()) 的输出:

    Array (
    
       [0] => something...
       [1] => something...
    )
    

    如果你想包含名称键而不是索引数组,你可以使用这个方法:

    <input type="text" value="something..." name="postArray[t1]" />
    <input type="text" value="something..." name="postArray[t2]" />
    

    print_r($this-&gt;input-&gt;post())的输出:

    Array (
    
       [t1] => something...
       [t2] => something...
    )
    

    【讨论】:

    • 请看我的更新。我正在使用 jquery .post 发送我的数据。我确定这是一个数组。 +1虽然。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2013-02-09
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2011-09-16
    • 2017-03-05
    相关资源
    最近更新 更多