首页 文章

在codeigniter视图中显示ajax发布的数组

提问于
浏览
0

我需要在从另一个视图发布到控制器的视图中显示数组的对象 .

Jquery ajax call

details 是一个包含很少对象的数组

$(document).ready(function () { // working
    $("#nxt").click(function() {
        var tmp = details;
        var more_details = JSON.stringify(tmp);
        $.ajax({
            type: "POST",
            url: 'http://localhost/application/index.php/Welcome/detailsLoad',
            data: {more_details : more_details },
            success : function(){
                console.log('Posted');
                location.href="http://localhost/application/index.php/Welcome/detailsLoad"
            },
            error: function(){
                alert('Error!');
            }
        });    
    });
});

Controller

public function detailsLoad()
{
        $moreDetails= $this->input->post('more_details');
        $this->load->view('simulation',$moreDetails);
}

View

<?php
      foreach($moreDetails['more_details'] as $result) {
          echo $result['object1'], '<br>';
          echo $result['object2'], '<br>';
      }
?>

帮我修改和修复这段代码

1 回答

  • 0

    如果要将ajax调用中的数据从控制器移动到视图,则应设置要在视图中使用的变量的名称:

    $data = [];
    $data['moreDetails'] = $this->input->post('more_details');
    $this->load->view('simulation',$data);
    

    然后你可以在视图中使用变量 $moreDetails

    foreach ($moreDetails as $result) {
        echo $result['object1'], '<br>';
        echo $result['object2'], '<br>';
    }
    

相关问题