简体   繁体   English

将ID值从视图传递到Codeigniter中的控制器

[英]pass a id value from view to controller in codeigniter

I can't pass id value from view to controller. 我无法将ID值从视图传递到控制器。

View Page: 查看页面:

<form>
  First name; <input type="text" name="firstname" id="firstname" value="">
  Last name:<input type="text" name="lastname" name="lastname" value="">    
 <a class="btn btn-info" href="<?php echo base_url();?>index.php/Inventory/issue/<?php echo $value->firstname; ?>" >PRINT</a>
</form>

Controller: 控制器:

public function issue($firstname){
   $this->Inventory_model->pick_Issue_model($firstname);
}

You can use codeigniter's URI class. 您可以使用codeigniter的URI类。

public function issue(){
   $firstname = $this->uri->segment(3);
   $this->Inventory_model->pick_Issue_model($firstname);
}

for reference : https://www.codeigniter.com/userguide3/libraries/uri.html 供参考: https : //www.codeigniter.com/userguide3/libraries/uri.html

 <a class="btn btn-info" href="<?php echo base_url();?>index.php/Inventory/issue?firstname=<?php echo $value->firstname; ?>" >PRINT</a>

现在,在控制器中,您可以将名字检索为:

$_GET['firstname'];

Instead of passing variable by url , POST values on your form submit.. 而不是通过url传递变量,而是在表单上提交POST值。

Change your form to 将表格更改为

<form action="<?php echo base_url();?>index.php/Inventory/issue/" method="POST">
  First name; <input type="text" name="firstname" id="firstname" value="">
  Last name:<input type="text" name="lastname" name="lastname" value="">    
 <button type="submit" class="btn btn-info"  >PRINT</button>
</form>

IN your Controller 在您的控制器中

public function issue(){
  $firstname = $this->input->post('firstname');
   $this->Inventory_model->pick_Issue_model($firstname);
}

Maybe you can use a hidden field in the form 也许您可以在表单中使用隐藏字段

<input type="hidden" name="fname" value="<?=$value->firstname?>"> 

Then in the controller 然后在控制器中

public function issue(){
   $postdata = $this->input->post();
   $firstname = $postdata['fname'];

   $this->Inventory_model->pick_Issue_model($firstname);
}

Your sending form in wrong way because it doesn't have submit type, But if you want to pass your data from link like one line in your code 您的发送表单使用了错误的方式,因为它没有提交类型,但是,如果您想从链接中传递数据,就像代码中的一行一样

<a class="btn btn-info" href="<?php echo base_url();?>index.php/Inventory/issue/<?php echo $value->firstname; ?>" >PRINT</a>

You are sending data to controller named Inventory to function named issue with parameter first_name so now you can see below code elaborate how to get data to controller 您正在将数据发送到名为Inventory的控制器,以参数first_name命名为issue的函数,因此现在您可以看到下面的代码详细说明了如何将数据获取到控制器

public function issue($firstname){
   $fname = $firstname;  //Do this
   $this->Inventory_model->pick_Issue_model($fname); // pass variable here
}

say $firstname stores golam and this name You send to controller's function but here you are calling model directly $this->Inventory_model->pick_Issue_model($firstname) so the model can't recognize where this $firstname comes from 比如说$firstname存储golam并且这个名字你发送到控制器的函数,但是在这里你直接调用模型$this->Inventory_model->pick_Issue_model($firstname)因此模型无法识别这个$firstname来源

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

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