将一个id值从视图传递给codeigniter中的控制器

时间:2017-10-04 08:10:45

标签: php codeigniter

我无法将id值从视图传递给控制器​​。

查看页面:

<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>

控制器:

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

5 个答案:

答案 0 :(得分:1)

您可以使用codeigniter的URI类。

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

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

答案 1 :(得分:0)

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

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

$_GET['firstname'];

答案 2 :(得分:0)

不是通过url传递变量,而是在表单上提交POST值..

将表单更改为

<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>

控制器

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

答案 3 :(得分:0)

也许你可以在表格中使用隐藏字段

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

然后在控制器中

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

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

答案 4 :(得分:0)

您的发送表单方式错误,因为它没有提交类型, 但是,如果您想从代码中的一行链接传递数据

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

您正在使用参数first_name将名为广告资源的数据发送到名为问题的功能 所以现在你可以看到下面的代码详细说明了如何将数据传输到控制器

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

$firstname存储golam并且这个名称你发送到控制器的功能但是在这里你直接调用模型$this->Inventory_model->pick_Issue_model($firstname)所以模型无法识别这个$firstname来自哪里

相关问题