0

hey guys i am new in codeigniter,i have a form like this

<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/$id" name="application" method="post" >
//some code
</form>

i have a controller methode

function input_investment($id)
{
$this->load->helper('form');
                $this->load->helper('html');
                $this->load->model('mod_user');
                $this->mod_user->insertinvestment($id);
}

i want to get $id from form action to controller methode how can i do that . . pls help me . .

Jay
  • 392
  • 4
  • 10
  • 28

4 Answers4

2

better to pass the value in the hidden field

<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name="my_id" value="<?php echo $id; ?>"/>
</form>

in your ci function

function input_investment() {
    $id = $this->input->post('my_id');
    $this->load->helper('form');
    $this->load->helper('html');
    $this->load->model('mod_user');
    $this->mod_user->insertinvestment($id);
}

or if you want (A test)

// Sample view

<?php $id = 1; ?>
<form action="<?php echo base_url('my_class/my_method/' . $id); ?>" method="post" >
  <input type="submit" />
</form>

// Controller

class My_class extends CI_Controller {

  public function index() {
    $this->load->view('my_class');
  }

  public function my_method($id) {
    echo $id; // outputs 1
  }

}
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
1

You need to use PHP and echo $id in the element if you want the value, right now you're sending '$id' to input_investment($id).

<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/<?php echo $id; ?>" name="application" method="post" >
//some code
</form>
Jakob Pogulis
  • 1,150
  • 2
  • 9
  • 19
0

Here your form method is post so you cont get the id through the get method ,you can do like

<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" > 
    <input type="hidden" name='id' value="<?php echo $id;?>">
</form>

and in your controller you can try with post like

$id = $_POST['id'];

or

$id = $this->input->post('id');

it willl better option for you in all cases if you are trying to send single or multiple data to the controller from an form....

GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • Sending data through POST instead of GET (or equal methods) has it advantages, but GET has its advantages as well, allowing the user to bookmark the page for example. – Jakob Pogulis Apr 01 '13 at 05:08
  • Yeh you are right,I will agree with you but i also told that "for multiple data sending through form is the best option" bec we cont send more values through URL as well .... – GautamD31 Apr 01 '13 at 05:10
0
$route['ctl_dbcont/input_investment/(:num)'] = "ctl_dbcont/input_investment/$1";

Just add this line at your config/route :) .

This will work with numbers only, if you have other type of IDs you can use (:any)

Other option is to catch the id directly using :

$id = $this->uri->segment(3);

Where segment(3) is the third element after your domain :

http://domain/segment1/segment2/segment3
Svetoslav
  • 4,686
  • 2
  • 28
  • 43