user3551487
user3551487

Reputation: 162

Passing Data from View to Controller (codeigniter)

I am trying to pass some Data using a link from the view to the Controller.

Controller:

public function details(){
    echo $this->input->post('data_id');
    $data['details'] = $this->wapi_db->details($this->input->post('data_id'));
    $data['latest_news'] = $this->wapi_db->latest_news();
    $data['main_content'] = "category/details";
    $this->load->view('includes/template',$data);
}

View:

<a href=<?php.base_url().'wapi/details?data_id='6'; ?>

Upvotes: 3

Views: 27975

Answers (3)

Christian Giupponi
Christian Giupponi

Reputation: 7618

You need to use

$this->input->get('data_id')

because you're passing data via GET method.

So your code should be:

public function details(){

    echo $this->input->get('data_id');

    $data['details'] = $this->wapi_db->details($this->input->get('data_id'));

    $data['latest_news'] = $this->wapi_db->latest_news();


    $data['main_content'] = "category/details";

    $this->load->view('includes/template',$data);


}

Here the docs: http://www.codeigniter.com/user_guide/libraries/input.html

Upvotes: 0

David
David

Reputation: 1145

I recommend you to use this link on your view

<a href=<?php.base_url().'wapi/details/6'; ?>

Then on your controller you just wait for the parameter on the function

public function details($data_id){//<- the parameter you want

    $data['details'] = $this->wapi_db->details($data_id);
    $data['latest_news'] = $this->wapi_db->latest_news();
    $data['main_content'] = "category/details";

    $this->load->view('includes/template',$data);

}

If your URI contains more than two segments they will be passed to your method as parameters.

From Codeigniter Controllers Guide

Upvotes: 1

Felix Kamote
Felix Kamote

Reputation: 342

You could simply us Uri segmen, just like this:

your View Code for href.

<a href="<?php echo base_url() ?>wapi/details/6";

And to GET the value you passed from view to controller You should do something like this in your controller

    public function details(){

        $data_id = $this->uri->segment(3); //this where you get you value from your link
        ...//rest of your code

    }

Upvotes: 6

Related Questions