使用codeigniter中的复选框删除

时间:2011-05-17 08:59:06

标签: php database codeigniter checkbox

我在codeigniter中删除复选框时遇到问题。我有一个表格,其中显示一个表格并填充数据库中的数据,并有两个提交按钮。第一个是我已经做过的adduser按钮。

我的问题是我不知道如何在显示的表格中获取复选框的值。以下是了解更多问题的片段:

控制器:

public function options()
    {
        $data['meta_title']="Users";
        $data['meta_desc']="Find your friends from our members";

        if ($this->input->post('AddUser'))
        {
            $this->load->view('template/header',$data);
            $this->load->view('users/add_view');
            $this->load->view('template/footer');
        }
        elseif($this->input->post('Delete'))
        {
            /// i have troubles with my logic here
        }
    }

查看:

<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo anchor('users/edit/'.$row['usrID'],$row['usrName']);?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>
            <td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['usrID'];?>" />

              <label for="checkID"></label></td>
        </tr>
<? endforeach; ?>

3 个答案:

答案 0 :(得分:0)

如果您实际上也添加了提交按钮,那么理解您的问题会更容易。

我会尽力向你解释这些原则; 您需要有一个表单来包装要发送的数据,例如:

<form action="whereToSendTheData.php" method="post">
    <table>
        <tr class="even gradeC">
            <td><?php echo anchor('users/edit/'.$row['usrID'],$row['usrName']);?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>
            <td><input type="checkbox" name="checkID" id="checkID" value="<?php echo $row['usrID'];?>" />

            <label for="checkID"></label></td>
        </tr>
    </table>
    <input type="submit" value="Add user">
</form>

此脚本会将表单中的数据发送到“whereToSendTheData.php”页面,我在上面的“action”属性中定义了该页面。

on“whereToSendData.php” 你需要一个脚本来处理/获取这些数据。 例如:

<?php
echo "hello $_POST['checkID']";
?>

将输出“hello”和checkID的值。

您可以在此处详细了解该主题: http://www.w3schools.com/php/php_post.asp

答案 1 :(得分:0)

您最好使用

<form method="post/get" action="url to ur controller">
   //some code to move data to controller
   <submit button>
</form>

并在您的控制器中放置一个删除功能:

public function delete()
{
   //get the id's of rows which are checked and delete them as
   $this->db->where('userid in','array(checked ids)');
}

答案 2 :(得分:0)

你做的一切都很棒,将输入名称设置为数组是正确的选择。

现在,要获取所选复选框的值,您可以这样做:

  

$ selectedUsers = $ this-&gt; input-&gt; get(&#39; checkID&#39;)//如果您已将表单方法设置为 GET

  

$ selectedUsers = $ this-&gt; input-&gt; post(&#39; checkID&#39;)//如果您已将表单方法设置为 POST

然后在你的模型中,如果你有这个功能

/**
 * @param array $values
 * @param string $field
 *
 * @return bool
 */
public function remove_in($values, $field)
{
    if ($values === false) {
        return FALSE;
    }

    $this->db->where_in($field, $values);

    return $this->db->delete($this->table);
}

您可以从控制器中调用它,如:

  

$ this-&gt; user_model-&gt; remove_in($ selectedUsers,&#39; user_id&#39;);

假设您的型号名称为 user_model ,您使用$this->load->model('user_model')加载了该名称,并且您的用户ID字段的名称为 user_id

享受!


NB:虽然这与您的情况无关,但您不应对多个HTML元素使用相同的ID。相反,如果没有必要,你可以省略id部分,如:

<input type="checkbox" name="checkID[]" value="<?php echo $row['usrID'];?>" />

或者如果您需要ID,您应该使用:

<input type="checkbox" name="checkID[]" id="checkID_<?php echo $row['usrID'];?>" value="<?php echo $row['usrID'];?>" />
相关问题