Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'; ?>
like image 727
user3551487 Avatar asked Dec 06 '25 10:12

user3551487


2 Answers

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

    }
like image 53
Felix Kamote Avatar answered Dec 07 '25 22:12

Felix Kamote


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

like image 36
David Avatar answered Dec 07 '25 22:12

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!