php checkbox默认选中,PHP+smarty对checkbox的初始化和绑定

在PHP+smarty中对checkbox进行加载时候,和其他的不一样.smarty有自带的checkbox自定义函数html_checkboxes.具体实现如下:

从接口取出数据绑定到checkbox:

第一种:

php:

//默认显示的checkbox选项

$responsibleDepartmentValue = array("收货组","IQC","仓储组","包货组","资料组","图片组");

//从接口查出来的数据然后用逗号分割成数组

$responsibleDepartmentSelectValue = array();

$xxxrepsonse = $Soap->getDepartment();

$responsibleDepartmentSelectValue = explode(',',$xxxrepsonse);

$smarty->assign("responsibleDepartmentValue", $responsibleDepartmentValue);

$smarty->assign("responsibleDepartmentSelectValue", $responsibleDepartmentSelectValue);

tpl:

{html_checkboxes name=selectResponseDepartmentCheckbox values=$responsibleDepartmentValue output=$responsibleDepartmentValue selected=$responsibleDepartmentValue separator=""}

name(用于post到后台获取数组)

values,checkbox中的value

output,checkbox中的显示字符

selected,已选定的元素

separator,checkbox之间的分隔符

labels,是否有标签,默认为true

最后页面生成结果:

收货组

IQC

仓储组

包货组

资料组

图片组

第二种:

php:

//默认显示的checkbox选项

$responsibleDepartmentValue = array(

[0]=>"收货组",

[1]=>"IQC",

[2]=>"仓储组",

[3]=>"包货组",

[4]=>"资料组",

[5]=>"图片组"

);

//从接口查出来的数据然后用逗号分割成数组

$responsibleDepartmentSelectValue = array();

$xxxrepsonse = $Soap->getDepartment();

$responsibleDepartmentSelectValue = explode(',',$xxxrepsonse);

$smarty->assign("responsibleDepartmentValue", $responsibleDepartmentValue);

$smarty->assign("responsibleDepartmentSelectValue", $responsibleDepartmentSelectValue);

tpl:

{html_checkboxes name=selectResponseDepartmentCheckbox options=$responsibleDepartmentValue selected=$responsibleDepartmentValue separator=""}

name,checkbox的name属性

selected,已选定的元素

options,包含值和显示的数组,可以是关联数组、也可以是数值数组

解析后的value=数组的下标,显示的字符为数组下标对应的值

separator,checkbox之间的分隔符

labels,是否有标签,默认为true

最终页面生成结果:

收货组

IQC

仓储组

包货组

资料组

图片组

PHP对smarty中checkbox的选中项获取:

使用了html_checkboxes后,函数会对name进行自动生成相应的[]html,以数组的形式传给php文件.

tpl:

{html_checkboxes name=selectResponseDepartmentCheckbox values=$responsibleDepartmentValue output=$responsibleDepartmentValue selected=$responsibleDepartmentValue separator=""}

php接收代码:

if(isset($_POST["selectResponseDepartmentCheckbox"])){

//将责任部门用逗号分割

$responsibleDepartment = implode(",",$_POST["selectResponseDepartmentCheckbox"]);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值