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

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

问题描述:

我无法将ID值从视图传递到控制器。传递一个id值从视图到codeigniter中的控制器

查看页:

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

请先阅读http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view – user4419336

+0

首先从您的url中删除index.php –

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

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

$_GET['firstname']; 
+0

如果它适合你,请给我一个upvote,因为我想恢复我的问题提问能力。 –

+0

显示:遇到的php错误 –

+0

现在删除echo base_url() 之后的问号现在一旦你在url中定义一个参数只能检索它的方法是使用$ _GET –

而是通过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); 
} 
+0

在窗体中我已经使用树按钮,其中一个保存按钮使用type = submit.now我想打印PDF文件....并在URL中调用herf = url .pass id值 –

你可以用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

也许你可以在表单中的控制器错误的方式,因为使用隐藏域

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

您发送表单它没有提交类型, 但是如果你想通过你的dat一个在你的代码的链接像一条线

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