Tedxxxx
Tedxxxx

Reputation: 258

Passing array data from view to controller and reverse php codeigniter

I am trying to pass the data from view to controller using php codeigniter. Here is my form view:

<form method="POST" action="<?php echo base_url()?>downloadfiles">
    <p>Download file</p>                    
    <p>Browse zip file</p> 
    <input type="text" name="data" value="<?= $data; ?>" style="display: none">                                     
    <input type="submit" name="downloadfiles" value="Download" />
</form> 

And in downloadfiles.php - the controller, I try to get the array $data passing by POST method and passing it back to the download_success.php view:

if ($this->input->post('downloadfiles')) {
        $data = $_POST['data'];          
        $this->load->view('upload/download_success', $data);
    }

This is my code in download_success.php:

<?php                
    if(!empty($data))
    {
       print_r($data);
    }
?>

When I run the code in the form view, it returns the error array to string conversion, and in the download_success view, it didn't print anything. Where were I wrong?

Upvotes: 2

Views: 5776

Answers (1)

Harish
Harish

Reputation: 462

This is because you are passing a string variable with the view and CodeIgniter consider it as an array.

MyController.php

if ($this->input->post('downloadfiles')) {
    $view_data = array('my_data' => $this->input->post('data') );          
    $this->load->view('upload/download_success', $view_data);
}

my_view.php

if(!empty($my_data))
{
   print_r($my_data);
}

Your form will be like:

//if $my_data is an array then you can print_r($my_data) and show the required values you want to show in your view.

<form method="POST" action="<?php echo base_url()?>downloadfiles">
    <p>Download file</p>                    
    <p>Browse zip file</p> 
    <input type="text" name="data" value="<?=$my_data?>">                                     
    <input type="submit" name="downloadfiles" value="Download" />
</form>

Try this and I hope it will help you.

Upvotes: 2

Related Questions