问题
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 . .
回答1:
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
}
}
回答2:
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>
回答3:
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....
回答4:
$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
来源:https://stackoverflow.com/questions/15737838/how-to-pass-id-in-controller-from-form-action-using-codeigniter