如何删除由提交的下拉列表选项定义的表格行(Codeigniter)

我有一个带有下拉菜单的表单,其中的选项从“dropdown_list”表中的“nama_guru”行填充。我想要做的是根据提交的下拉菜单选项删除该表中的一行“nama_guru”。


这是我的一些代码。


下拉式菜单


<div class="left-inner-addon">

    <select input type="text" name="nama_guru" value="<?php echo $nama_guru ?>" class="form-control" id="inputNama" >

        <?php 

        foreach($nama_guru as $row)

        { 

            echo '<option value="'.$row->nama_guru.'">'.$row->nama_guru.'</option>';

        }

        ?>

    </select>

</div>

控制器


function data_kuesioner_save($idx = '', $where =''){

    if($_POST){

        $nama_guru          = $this->input->post('nama_guru');


        if($status == 'baru'){

            $data = array(

                'nama_guru' => $nama_guru,

            );

            $sukses = '

                <div class="alert alert-success">

                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

                  <strong>Sukses!</strong> Data anda telah tesimpan.

                </div>

            ';

            $this->siswamodel->deletedropdowndata($nama_guru);

            $this->session->set_flashdata('sukses', $sukses);

            $this->siswamodel->insertdata('data_kuesioner',$data);

            redirect('siswa/data_kuesioner_view');

        }

    }

}

模型


class Siswamodel extends CI_Model {


    function insertdata($tabel, $data){

        return $this->db->insert($tabel,$data);

    }


    function deldata($tabel,$where){

        return $this->db->delete($tabel,$where);

    }


    function updatedata($tabel,$data,$where){

        return $this->db->update($tabel,$data,$where);

    }


    function selectdata($where = ''){

        return $this->db->query("select * from $where;");

    }


    function printjointable($where = ''){

        return $this->db->query("select * from $where;");

    }


    function deletedropdowndata($idx){

        $this->db->where('nama_guru', $idx);

        $this->db->delete('isi_dropdown1');

    }

}


侃侃无极
浏览 126回答 2
2回答

喵喵时光机

欢迎来到 SOF!首先,我们需要更正我们的下拉列表。我们不会为我们的 select DOM 元素提供输入类型,它已经设置为“select”,这意味着下拉列表。其次, value 属性用于不是元素本身的选项。请看下面的代码。<div class="left-inner-addon"><select name="nama_guru" class="form-control" id="inputNama" >&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; foreach($nama_guru as $row)&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo '<option value="'.$row->nama_guru.'">'.$row->nama_guru.'</option>';&nbsp; &nbsp; }&nbsp; &nbsp; ?></select></div>好的,我们已经修复了我们的标记。现在处理删除部分。如果没有表单,我们就无法做到这一点,我相信您有类似下面的内容,它封装了您的其余标记。<form action="" method="post">&nbsp; // Form Content ...</form>当我们不提及任何操作时,表单将提交给自身,这意味着我们可以在用于输出表单的同一函数中捕获请求。让我们处理您的控制器功能public function data_kuesioner_view(){&nbsp; &nbsp; if($_POST)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Delete data from table&nbsp; &nbsp; &nbsp; &nbsp; $this->siswamodel->deletedropdowndata($_POST);&nbsp; &nbsp; &nbsp; &nbsp; // Success Message&nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('success', 'Congratulations, Data removed.');&nbsp; &nbsp; &nbsp; &nbsp; // Redirect to the form&nbsp; &nbsp; &nbsp; &nbsp; redirect(base_url().'siswa/data_kuesioner_view');&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // load form view&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('form');&nbsp; &nbsp; }}// Model functionpublic function deletedropdowndata($data){&nbsp; &nbsp; $this->db->where('nama_guru', $data['nama_guru']);&nbsp; &nbsp; $this->db->delete('isi_dropdown1');&nbsp; &nbsp; return true;}让我们显示我们的 flash 消息表格上方 只需将代码放在下方即可;<?php if(!empty($this->session->flashdata('success'))){?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-success">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php print_r($this->session->flashdata('success'));?>&nbsp; &nbsp; &nbsp; &nbsp; </div><?php }?>

qq_笑_17

返回查询function deletedropdowndata($idx){&nbsp; return $this->db->where('nama_guru', $idx)->delete('isi_dropdown1');&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP