user3457998
user3457998

Reputation: 37

passing the input name (comes from database) from view to controller

I'm trying to learn codeigniter, the first thing i did is to make simple add edit delete function...

I'm having trouble calling the name of input button to the controller to delete an entire row of table..

model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Delete_model extends CI_Model{
  public function delete($data){
    $this->db->delete('record_tbl', $data);
  }
}

controller

class Delete_controller extends CI_Controller {
  public function index()
  {
    $this->delete();
  }

  public function delete()
  {
    $data = array();
    $this->load->model('fetch');
    $query = $this->fetch->getData();
    if ($query)
    {
      $data['results'] = $query;
    }

    $this->load->view('delete', $data);
  }

  public function delete_data(){
    $this->load->model('delete_model');
    $where = $this->input->get('id');
    $data['id'] = $where;
    $this->delete_model->delete($data);
    redirect('');
  }
}

view

<table border="1" width="100%">
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Phone Number</th>
    <th>Status</th>
    <th></th>
  </tr>
  <?php foreach($results as $row) { ?>
  <tr>
  <td><?=$row->name?></td>
  <td><?=$row->addr?></td>
  <td><?=$row->pnum?></td>
  <td><?=$row->status?></td>
  <?php echo form_open('delete_controller/delete_data') ?>
  <td style="text-align: center"><input name="<?=$row->id?>" type="submit" value="delete"></input>
  </form>
  </td>
  </tr>
<?php } ?>
</table>

Upvotes: 1

Views: 60

Answers (3)

YouSer
YouSer

Reputation: 393

Instead of $this->input->get('id'); use $this->input->post('id');.

And to simplify things up: I probably recommend using <button></button> as control buttons like delete functionality (it also works on edit/update FYI).

Coz you can just add attribute to a button value = $id and name=delete. And it would be like an <input type='submit'> just by adding an attribute of type='submit'

So

VIEW

<table border="1" width="100%">
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Phone Number</th>
    <th>Status</th>
    <th></th>
  </tr>
  <?php foreach($results as $row) { ?>
  <tr>
  <td><?=$row->name?></td>
  <td><?=$row->addr?></td>
  <td><?=$row->pnum?></td>
  <td><?=$row->status?></td>
  <?php echo form_open('delete_controller/delete_data') ?>
  <td style="text-align: center">
  <?php
     echo "<button type='submit' name='delete' value='" . $row->id . "'>" ;
     echo "DELETE" ;
     echo "</button>" ;
  ?>
  </form>
  </td>
  </tr>
<?php } ?>
</table>

Then it would be very simple to fetch the id of the row you want to delete. Just do this in your controller $id = $_POST['delete']; or $id = $this->input->post('delete');

Upvotes: 0

Hans
Hans

Reputation: 3523

It's going to be $this->input->post ('id') like @Pattle says and you are using the "name" attribute of the form for the ID. What I usually do is use a hidden field (see form_hidden () helper in CI documentation) with the name "id" and the value you want ($row->id).

<?php echo form_hidden ('id', $row->id); ?>

Upvotes: 0

Pattle
Pattle

Reputation: 6016

Your form will be doing a POST rather than a GET request. So rather than doing

$where = $this->input->get('id');

You want to do

$where = $this->input->post('id');

Also why don't you pass the value of $row->id to your controller so that you know which input field to target. So in your view do

<?php echo form_open('delete_controller/delete_data/' . $row->id) ?>

Then in your controller

public function delete_data($id){
    $this->load->model('delete_model');
    $where = $this->input->post($id);
    //...

Upvotes: 1

Related Questions