简体   繁体   English

如何使用Codeigniter通过表单操作在控制器中传递ID

[英]how to pass id in controller from form action using codeigniter

hey guys i am new in codeigniter,i have a form like this 嘿,我是Codeigniter的新手,我有这样的表格

<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 . 我想从表单操作到控制器方法获取$ id,我该怎么做。 . pls help me . 请帮助我 。 .

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 在您的ci功能中

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
  }

}

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). 如果需要该值,则需要使用PHP并在元素中回显$ id,现在您要向input_investment($ id)发送“ $ 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>

Here your form method is post so you cont get the id through the get method ,you can do like 在这里,您的form方法是post,因此您可以通过get方法来获取id,您可以像

<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.... 如果您尝试从表单向控制器发送单个或多个数据,则在所有情况下都将是一个更好的选择。...

$route['ctl_dbcont/input_investment/(:num)'] = "ctl_dbcont/input_investment/$1";

Just add this line at your config/route :) . 只需在您的config / route :)中添加此行。

This will work with numbers only, if you have other type of IDs you can use (:any) 这仅适用于数字,如果您具有其他类型的ID,则可以使用(:any)

Other option is to catch the id directly using : 另一种选择是直接使用捕获ID:

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

Where segment(3) is the third element after your domain : 其中segment(3)是您网域之后的第三个元素:

http://domain/segment1/segment2/segment3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM