Codeigniter复选框数组 [英] Codeigniter checkbox array

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

问题描述

我的复选框如下所示:

 < div& 
< input type =checkboxname =fruits []value =apple<?php echo set_checkbox('fruits []','apple'); ?> />
< input type =checkboxname =fruits []value =banana<?php echo set_checkbox('fruits []','banana'); ?> />
< input type =checkboxname =fruits []value =peach<?php echo set_checkbox('fruits []','peach'); ?> />
< / div>

我在名称中使用数组,以便我可以填充一个文本字段我选择的复选框的值。我使用的是stackoverflow上的脚本。

 函数calculate(){

var fruit = $ .map($('input:checkbox:checked'),function(e,i){
return e.value;
}

$('#fruits')。val(fruit.join(',')); //添加以逗号分隔的值到#fruits input
}

calculate();

$('div')。delegate('input:checkbox','click',calculate);

到目前为止,它的工作原理,但我有麻烦设置验证规则的复选框,因为他们都有一样的名字。我试过..

  $ this-> form_validation-> set_rules('fruits []','Apple' required | xss_clean'); 
$ this-> form_validation-> set_rules('fruits []','Banana','required | xss_clean');
$ this-> form_validation-> set_rules('fruits []','Peach','required | xss_clean');

并使用 echo form_error('fruits []'); 显示验证错误,但没有任何反应。最终,我想在提交的状态下显示一个复选框,如果我的窗体中的另一个字段出现错误。而是取消选中所有复选框。我不知道如何解决这个问题。



POST

  Array([FRUITS] => Array([0] => apple [1] => banana [2] => peach)


这是检查时

解决方案

不使用方括号[ ,所以只是'水果'。



[]只是对浏览器说将这些值作为一个数组,而不是一个值,因为他们是组,但是在PHP中,你只需将它作为任何其他值,即$ _POST ['fruits']。



希望有帮助,



所有最好的,
NwN


My checkboxes look like this

<div>
    <input type="checkbox" name="fruits[]" value="apple" <?php echo set_checkbox('fruits[]', 'apple'); ?> />
    <input type="checkbox" name="fruits[]" value="banana" <?php echo set_checkbox('fruits[]', 'banana'); ?> />
    <input type="checkbox" name="fruits[]" value="peach" <?php echo set_checkbox('fruits[]', 'peach'); ?> />
</div>

I am using array in the names so that I can populate a text field (via jquery) with the values of the checkboxes I select. I am using a script found on stackoverflow.

function calculate() {

        var fruit = $.map($('input:checkbox:checked'), function(e, i) {
            return e.value;
        });

        $('#fruits').val(fruit.join(',')); //adds values separated by comma to #fruits input 
    }

    calculate();

    $('div').delegate('input:checkbox', 'click', calculate);

So far it works but I'm having trouble setting validation rules for the checkboxes because they all have the same name. I tried..

$this->form_validation->set_rules('fruits[]', 'Apple', 'required|xss_clean');
$this->form_validation->set_rules('fruits[]', 'Banana', 'required|xss_clean');
$this->form_validation->set_rules('fruits[]', 'Peach', 'required|xss_clean');

and used echo form_error('fruits[]'); to display validation error but nothing happens. Ultimately I want to display a checkbox in the state it was submitted, should an error occur on another field in my form. Instead all the checkboxes uncheck. I'm not sure how to fix this.

POST

Array ( [FRUITS] => Array ( [0] => apple [1] => banana [2] => peach )

this is when they are checked

解决方案

Try without using the square brackets [], so just 'fruits'.

The [] are just to say to the browser "post these values as an array, not a single value, because they're a group", but in PHP you would just approach it as any other value, viz. $_POST['fruits'].

Hope that helps,

All the best, NwN

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

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