Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中获取复选框值_Php_Codeigniter_Checkbox - Fatal编程技术网

Php 如何在codeigniter中获取复选框值

Php 如何在codeigniter中获取复选框值,php,codeigniter,checkbox,Php,Codeigniter,Checkbox,朋友们好,我对codeigniter中的复选框有问题。我想通过单击带有ID的复选框向不同的朋友发送多条消息 这是我的看法 <tr> <td><input type="checkbox" name="friend_id[]" value=<?php echo $friend->id;?>></td>

朋友们好,我对codeigniter中的复选框有问题。我想通过单击带有ID的复选框向不同的朋友发送多条消息

这是我的看法

                    <tr>
                        <td><input type="checkbox" name="friend_id[]" value=<?php echo $friend->id;?>></td>
                        <td><?php echo $friend->friend_name;?></td>
                    </tr>

>
这是我的控制器

public function message(){

$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id','Recipients', 'required');
if($this->form_validation->run()){
     $data = $this->input->post('friend_id');
     echo '<pre>';
     print_r($data);
     echo '</pre>';
     }
     else{
     echo validation_errors();
        }
     }
public函数消息(){
$this->form_validation->set_规则('message','message','required');
$this->form_validation->set_规则('friend_id','Recipients','required');
如果($this->form\u validation->run()){
$data=$this->input->post('friend_id');
回声';
$this->form_validation->set_rules('friend_id','Recipients', 'required');
打印(数据); 回声';
$this->form_validation->set_rules('friend_id','Recipients', 'required');
} 否则{ 回显验证_错误(); } }
当我运行它时..我得到一个错误,显示在我检查姓名时收件人是必需的

如果确实使用数组作为字段名,则必须使用精确的数组 需要字段名的助手函数中的名称,以及 验证规则字段名

因此,请尝试:

 public function message(){

        $this->form_validation->set_rules('message','Message', 'required');
        $this->form_validation->set_rules('friend_id','Recipients', 'callback_arr_required');
        if($this->form_validation->run()){
             $data = $this->input->post('friend_id');
             echo '<pre>';
             print_r($data);
             echo '</pre>';
             }
             else{
             echo validation_errors();
                }
             }

    public function arr_required($arr) {

        if (is_array($arr) && count($arr) > 0) {
            return true;
        }
        $this->form_validation->set_message('arr_required', 'Please select atleast one friend.');
        return FALSE;

    }
否则:

$this->form_validation->set_rules('friend_id[]', 'Recipients', 'required');
public函数消息(){
$this->form_validation->set_规则('message','message','required');
$this->form_validation->set_规则('friend_id','Recipients','callback_arr_required');
如果($this->form\u validation->run()){
$data=$this->input->post('friend_id');
回声';
$this->form_validation->set_rules('friend_id','Recipients', 'required');
打印(数据); 回声';
$this->form_validation->set_rules('friend_id','Recipients', 'required');
} 否则{ 回显验证_错误(); } } 需要公共功能arr_($arr){ if(is_数组($arr)和计数($arr)>0){ 返回true; } $this->form_validation->set_message('arr_required','请选择至少一个朋友'); 返回FALSE; }
使用此代码进行测试

$this->form_validation->set_rules('friend_id[]','Recipients', 'required');

在codeigniter验证中,更改以下行:


这个问题会得到解决。

你的答案为我节省了很多时间…祝福你的答案为我节省了很多时间…祝福