首页 文章

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

提问于
浏览
0

我无法将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

    您可以使用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

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

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

    $_GET['firstname'];
    
  • 1

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

    <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);
    }
    
  • 0

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

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

    您正在将名为 Inventory 的控制器发送到名为 issue 的函数,其参数为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 来自哪里

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

    在你的 Controller

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

相关问题