2

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?

3
  • In your controller replace $data = $_POST['data']; with $data = array('my_data' => $_POST['data'] ); and in view replace $data with '$my_data' Commented Sep 22, 2017 at 2:32
  • I did and it returned the same error. Commented Sep 22, 2017 at 2:45
  • I have updated the script in my answer below. Commented Sep 22, 2017 at 2:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I also printed $my_data array in my form to test the data in the array. It printed correctly. But in my_view.php it doesn't print anything.
Hey updated is the code I have executed at my local system but it's not giving me this error.
Ok, are you getting an error or just empty $my_data ?
it just return empty $my_data.
In your form you make the input field hidden so how are you passing the value in it then?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.