PHP+smarty对checkbox的初始化和绑定

来源:互联网 发布:上海知楚 旻泉 编辑:程序博客网 时间:2024/06/07 10:11

在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,是否有

最后页面生成结果:

<label><input name="selectResponseDepartmentCheckbox[]" value="收货组" type="checkbox">收货组</label><label><input name="selectResponseDepartmentCheckbox[]" value="IQC" type="checkbox">IQC</label><label><input name="selectResponseDepartmentCheckbox[]" value="仓储组" type="checkbox">仓储组</label><label><input name="selectResponseDepartmentCheckbox[]" value="包货组" type="checkbox">包货组</label><label><input name="selectResponseDepartmentCheckbox[]" value="资料组" type="checkbox">资料组</label><label><input name="selectResponseDepartmentCheckbox[]" value="图片组" type="checkbox">图片组</label> 

第二种:
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,是否有

最终页面生成结果:

<label><input name="selectResponseDepartmentCheckbox[]" value="0" type="checkbox">收货组</label><label><input name="selectResponseDepartmentCheckbox[]" value="1" type="checkbox">IQC</label><label><input name="selectResponseDepartmentCheckbox[]" value="2" type="checkbox">仓储组</label><label><input name="selectResponseDepartmentCheckbox[]" value="3" type="checkbox">包货组</label><label><input name="selectResponseDepartmentCheckbox[]" value="4" type="checkbox">资料组</label><label><input name="selectResponseDepartmentCheckbox[]" value="5" type="checkbox">图片组</label> 

PHP对smarty中checkbox的选中项获取:
使用了html_checkboxes后,函数会对name进行自动生成相应的[]html,以数组的形式传给php文件.
tpl:

<form  method="POST" action="/index.php">{html_checkboxes name=selectResponseDepartmentCheckbox values=$responsibleDepartmentValue output=$responsibleDepartmentValue selected=$responsibleDepartmentValue separator=""} </form>

php接收代码:

  if(isset($_POST["selectResponseDepartmentCheckbox"])){         //将责任部门用逗号分割         $responsibleDepartment = implode(",",$_POST["selectResponseDepartmentCheckbox"]);   }
1 0
原创粉丝点击