在codeigniter视图中显示ajax发布的数组 [英] Display ajax posted array in codeigniter view

查看:115
本文介绍了在codeigniter视图中显示ajax发布的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Jquery ajax调用 / p>

详细是一个具有少许对象的数组

  $(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
},
错误:function(){
alert('Error!');
}
});
});
});

控制器

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

查看

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

帮助我修改和修复此代码

解决方案

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

  $ data = []; 
$ data ['moreDetails'] = $ this-> input-> post('more_details');
$ this-> load-> view('simulation',$ data);然后你可以使用变量 $ moreDetails


$ b <内部视图:

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


I need to display objects of an array in a view that has been posted to controller from another view.

Jquery ajax call

details is an array with few objects

$(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>';
      }
?>

help me to modify and fix this code

解决方案

If you want to data from the ajax call to move from your controller to your view you should set the name of the variable to be used inside your view:

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

Then you can use the variable $moreDetails inside the view:

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

这篇关于在codeigniter视图中显示ajax发布的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆