在Codeigniter中获取复选框的多个值 [英] Get multiple values of checkbox in codeigniter

查看:55
本文介绍了在Codeigniter中获取复选框的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个项目。我有一个复选框(用户可以在其中选择公司提供的服务类型)。当我尝试在控制器中发布选定的服务(例如,选中了2个服务)时,我只能得到一个服务。问题是如何在复选框中获取多个值?

I am currently doing a project. I have a checkbox( where the user will choose type of services provided by the company). When I try to post the service that was selected(for example 2 services is checked) in my controller, I am only getting one service. The question is how can I get the multiple values in my checkbox?

注意:我也尝试在控制器中使用foreach,但出现类似无效参数的错误为foreach()提供。

Note: I also tried to use foreach within my controller, I am getting some error like "Invalid argument supplied for foreach()".

查看

<label>Desired Service</label> <br>
<?php foreach($services as $s):?>
<label><input type="checkbox" name="service_name[]" value="<?= $s->service_name?>"><?= $s->service_name?></label>
<br> 
<?php endforeach?>

控制器

$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
    $this->form_validation->set_rules('full_name', 'Fullname', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('contact', 'Contact', 'required');
    $this->form_validation->set_rules('date', 'Date', 'required');
    $this->form_validation->set_rules('address', 'Address', 'required');
    $this->form_validation->set_rules('zip_code', 'Zip Code', 'numeric|required');
    $this->form_validation->set_rules('province', 'Province', 'required');
    $this->form_validation->set_rules('date', 'Date', 'required');
    $this->form_validation->set_rules('service_name', 'Service', 'required');
    if ($this->form_validation->run() == FALSE) {
        $this->index();
    } 
    else {
        $service_name = implode(', ', $_POST['service_name']);
        $event = array(
        'full_name' => $this->input->post('full_name'),
        'email' => $this->input->post('email'),
        'contact' => $this->input->post('contact'),
        'address' => $this->input->post('address'),
        'zip_code' => $this->input->post('zip_code'),
        'state_province' => $this->input->post('province'),
        'date' => $this->input->post('date'),
        'service' => $service_name

        );
        $this->EventModel->add_event($event);
        echo "<script>
        window.alert('Your Desired Date is being Proccessed!');
        location.href = '".site_url('/')."';
        </script>";
    }


推荐答案

更改为

$service_name = $_POST['service_name'];
foreach($service_name as $key =>$value)
{
    echo $value;
}
die;

$service_name = implode(',',$_POST['service_name']);
echo $service_name;

这篇关于在Codeigniter中获取复选框的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆