php 如何使用codeigniter从表单动作在控制器中传递id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15737838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:52:10  来源:igfitidea点击:

how to pass id in controller from form action using codeigniter

phpcodeignitercodeigniter-2codeigniter-routingcodeigniter-form-helper

提问by Jay

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 . . pls help me . .

我想从表单操作中获取 $id 到控制器方法,我该怎么做。. 请帮助我 。.

回答by Dino

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
  }

}

回答by Jakob Pogulis

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,现在您正在将 '$id' 发送到 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>

回答by Gautam3164

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

在这里,您的表单方法已发布,因此您可以通过 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....

如果您尝试从表单向控制器发送单个或多个数据,那么在所有情况下它都会为您提供更好的选择....

回答by Svetoslav

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

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 = $this->uri->segment(3);

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

其中 segment(3) 是域之后的第三个元素:

http://domain/segment1/segment2/segment3