golam moktadir
golam moktadir

Reputation: 49

pass a id value from view to controller in codeigniter

I can't pass id value from view to controller.

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

Upvotes: 0

Views: 2696

Answers (5)

Abhijit Kumbhar
Abhijit Kumbhar

Reputation: 961

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

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

Upvotes: 0

Kevin R
Kevin R

Reputation: 75

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

Upvotes: 0

geeth
geeth

Reputation: 714

You can use codeigniter's URI class.

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

Upvotes: 1

Yadhu Babu
Yadhu Babu

Reputation: 1521

Instead of passing variable by url , POST values on your form submit..

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

Upvotes: 0

Sushant Bassi
Sushant Bassi

Reputation: 379

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

Now in controller you can retrieve the firstname as :

$_GET['firstname'];

Upvotes: 0

Related Questions