简体   繁体   中英

passing array from view using ajax to another view codeigniter

Code segments of my application are added below. myArray is an array and each element contains few objects. I need to use that array in 2nd view.

When I alert(myJSON); getting the array in alert window. When the view loads it displays NULL .

Ajax call on my first view

$(document).ready(function () {

$("#btn-click").click(function(){

    var tmp = myArray;
    var details= JSON.stringify(tmp);

    $.ajax({
        type: "POST",
        url: 'http://localhost/Application1/index.php/Welcome/loadDetails',
        data: {details: details},
        success : function(myJSON){
            alert(myJSON);
            console.log('Posted');
            location.href="http://localhost/Application1/index.php/Welcome/loadDetails"

        },
        error: function(){
            alert('Error!');
        }
    });    
 });
});

Controller

public function loadDetails()
{
            $data = [];
            $data['load_details'] = $this->input->post('details');
    $this->load->view('load_view',$data);
}

2nd View

var_dump($load_details);

What are the changes that I should do to display passed arary in my 2nd view.

If you want to get data in 2nd view then you need to call ajax one more time in success instead of location.href .(Using location.href, you will not get data in loadDetails() method . )

Try with this :

$.ajax({
        type: "POST",
        url: 'http://localhost/Application1/index.php/Welcome/loadDetails',
        data: {details: details},
        success : function(myJSON){
            alert(myJSON);
            console.log('Posted');
             $.ajax({
                type: "POST",
                url: 'http://localhost/Application1/index.php/Welcome/loadDetails',
                data: {details: details},
                success : function(mydata){
                    alert(mydata);
                }
            }); 
        },
        error: function(){
            alert('Error!');
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM