+ -
当前位置:首页 → 问答吧 → 处理表单函数(options,radios,checkboxs)

处理表单函数(options,radios,checkboxs)

时间:2007-09-16

来源:互联网

在项目开发中,对于表单的处理是必不可少的一个处理,还在为处理复杂表单而烦恼吗?
其实程序员在工作会收集很多相关的处理函数,在工作中项目开发周期开发效率提高很多,关于表单的处理PHP提供了很多各式各样的处理函数,我们是不是应该合理利用,以便给我们以后的开发带来方便呢?

做为处理表单大家会有自己的一套方法,不论是基于各种框架本身的处理机制或是类库,函数库...
在这里我把我写的三个表单元素的处理函数做一下说明,当然了,函数本身还有很多缺陷,在这里还需要改善.希望大家多多提出宝贵意见和心得体会。

以下三个函数是处理options(下拉列表),radios(单选安钮),checkboxs(多选安钮)的处理函数。

用过模板引型开发项目的朋友对数组的构造应该是在熟悉不过了吧。在做标签结合模板也面输出时,数组的构造对于我们布置标签带来了很多方便,在这里我也是根据数组去构造去构造,来输出,不论是直接调用还是运用到模板一样很方便。

引用:
<?php
/*
* @copyright Copyright (c) 2007 ASEN (bbs.54php.com)
* @author  特蓝克斯
* @package Function
* @date    2007-09-11
* @return  Array
* @Notes   处理表单 Options,Radio,Checkboxs 表单控件
*/

/**
*
* getOptions 参数列表:
* - result        Options 数组
* - vals        Options 数组修改的键值 (Value)
* - except        Options 除"except"值外的数据调用
* - same        Value和Title 值是否相同
* - exceptKey    Options 除"exceptKey"键外的数据调用
*
* @return Array() $options
*/

function getOptions($result,$vals='whatthehellisthis',$except='',$same='',$exceptKey='') {
    if(!$result) return false;
    $options = "";
    while(list($key,$val) = each($result)) {
        $checkVal = false;
        if($except) {
            if(is_array($except)) $checkVal = in_array($val,$except);
            else {
                if($except == $val) $checkVal = true;
            }
        }
        if($exceptKey) {
            if(is_array($exceptKey)) $checkVal = in_array($key,$exceptKey);
            else {
                if($exceptKey == $key) $checkVal = true;
            }
        }
        if(!$checkVal) {
            if($same) $key = $val;
            $options .= "<option value='".$key."'";
            if($key == $vals) $options .= " selected";
            $options .= ">".$val."</option>\n";
        }
    }
    return $options;
}

/**
*
* getRadios 参数列表:
* - result        Radio 数组
* - name        Radio 名字
* - checked    默认选中第一个,通读索引控制 Radio 的 Checked 状态
* - vals        通过VALUE控制 Radio 的 Checked 状态
* - except        Radios 除"except"值外的数据调用
* - enter        换行参数
* - Events        事件参数
* - style        样式参数
* - disabled    显示参数
* - exceptKey    Radios 除"exceptKey"键外的数据调用
*
* @return Array() $radios
*/
function getRadios($result ,$name="radio" ,$checked='0' ,$vals='whatthehellisthis' ,$except='' ,$enter='0', $Events='' ,$style='border:solid 1 #FFFFFF;' ,$disabled='' ,$exceptKey='') {
    if(!$result) return false;
    $radios = '';
    $i = 0;

     foreach ($result as $key => $val) {
        $checkVal = false;
         
        if($except) {
            if(is_array($except)) $checkVal = in_array($val,$except);
            else {
                if($except == $val) $checkVal = true;
            }
        }
        if($exceptKey) {
            if(is_array($exceptKey)) $checkVal = in_array($key,$exceptKey);
            else {
                if($exceptKey == $key) $checkVal = true;
            }
        }

        if(!$checkVal) {
            $radios .= "<input name='".$name."' type='radio' value='".$key."'";
            if($key == $vals) $radios .= " checked";
            if ($checked != 'no' && $checked == $i) {
                $radios .= " checked";
            }
            if ($disabled != "" && $disabled[$i] == 1) {
                $radios .= " disabled";
            }
            $radios .= " style='".$style."' ".$Events.">".$val." ";
        }
         
        $i++;
            if ($enter !=0 && ($i % $enter) ==0) {
                $radios .="<Br>";
            }
    }

    return $radios;
}

/**
*
* getCheckboxs 参数列表:
* - result        Checkbox 数组
* - name        Checkbox 名字
* - checked    默认选中第一个,通读索引控制 Checkbox 的 Checked 状态
* - vals        通过VALUE控制 Checkbox 的 Checked 状态
* - except        Checkboxs 除"except"值外的数据调用
* - enter        换行参数
* - Events        事件参数
* - style        样式参数
* - disabled    显示参数
* - exceptKey    Checkboxs 除"exceptKey"键外的数据调用
*
* @return Array() $Checkboxs
*/
function  getCheckboxs($result,$name='Checkboxs', $checked='0', $vals='whatthehellisthis',$except='',$enter='0',$Events='',$style='' ,$disabled='',$exceptKey='') {
    if(!$result) return false;
    $Checkboxs = "";
    $i = 0;

     foreach ($result as $key => $val) {
        $checkVal = false;

        if($except) {
            if(is_array($except)) $checkVal = in_array($val,$except);
            else {
                if($except == $val) $checkVal = true;
            }
        }
        if($exceptKey) {
            if(is_array($exceptKey)) $checkVal = in_array($key,$exceptKey);
            else {
                if($exceptKey == $key) $checkVal = true;
            }
        }
        if(!$checkVal) {
            $Checkboxs .= "<input name='".$name."' type='Checkbox' value='".$key."'";
            if(is_array($vals) && array_key_exists($key,$vals)) $Checkboxs .= " checked";
            if(!is_array($vals) && $key == $vals)  $Checkboxs .= " checked";
            if ($checked != 'no' && $checked == $i) {
                $Checkboxs .= " checked";
            }
            if ($disabled<>"" && $disabled[$i] ==1) {
                $Checkboxs .= " disabled";
            }
            $Checkboxs .= " style=".$style." ".$Events.">".$val." ";
        }

        $i++;
            if ($enter !=0 && ($i % $enter) ==0) {
                $Checkboxs .="<Br>";
            }
    }

    return $Checkboxs;
}

?>

作者: 特蓝克斯   发布时间: 2007-09-15

引用:
<?php
/*
* @copyright Copyright (c) 2007 ASEN (bbs.54php.com)
* @author  特蓝克斯
* @package Function
* @date    2007-09-11
* @return  Array
* @Notes   处理表单 Options 表单控件
*/
include_once "helpers_forms.php";


$arr_getoptions = array(
//                        'key'        => 'value',
                        '54_name'    => "54master",
                        '54_title'    => "网络编程",
                        '54_group'    => "本版讨论群: 19870468"
);

$option  = getOptions($arr_getoptions);
$option1 = getOptions($arr_getoptions, '', '网络编程');
$option2 = getOptions($arr_getoptions, "54_title");
$option3 = getOptions($arr_getoptions, "54_title", '',true);
$option4 = getOptions($arr_getoptions, '', '', '', '54_title');
?>
添加调用:
<select name="getoption">
<?=$option?>
</select>
<Br>
<textarea style="width:500;height:120">
<select name="getoption">
<?=$option?>
</select>
</textarea>

<br><br>
除VALUE值为"网络编程"的全部调用:
<select name="getoption1">
<?=$option1?>
</select>
<Br>
<textarea style="width:500;height:120">
<select name="getoption1">
<?=$option1?>
</select>
</textarea>

<br><br>
修改时调用:
<select name="getoption2">
<?=$option2?>
</select>
<Br>
<textarea style="width:500;height:120">
<select name="getoption2">
<?=$option2?>
</select>
</textarea>

<br><br>
value值和title值相等时 :
<select name="getoption3">
<?=$option3?>
</select>
<Br>
<textarea style="width:500;height:120">
<select name="getoption3">
<?=$option3?>
</select>
</textarea>

<br><br>
除KEY值为"54_title"全部调用:
<select name="getoption4">
<?=$option4?>
</select>
<Br>
<textarea style="width:500;height:120">
<select name="网络编程">
<?=$option4?>
</select>
</textarea>

作者: 特蓝克斯   发布时间: 2007-09-15

引用:
<?php
/*
* @copyright Copyright (c) 2007 ASEN (bbs.54php.com)
* @author  特蓝克斯
* @package Function
* @date    2007-09-11
* @return  Array
* @Notes   处理表单 Radio 表单控件
*/
include_once "helpers_forms.php";


$arr_radio = array(
//                        'key'        => 'value',
                        '54_name'    => "54master",
                        '54_title'    => "网络编程",
                        '54_group'    => "本版讨论群: 19870468"
);

$radio  = getRadios($arr_radio);
$radio1 = getRadios($arr_radio,'radio1','no');
$radio2 = getRadios($arr_radio, 'radio2', 'no', '54_group');
$radio3 = getRadios($arr_radio, 'radio3', '', '' ,'网络编程');
$radio4 = getRadios($arr_radio, 'radio4', '', '' , '', '', '', '', '' ,'54_title');
?>
默认(选中第一个"radio"):
<?=$radio?><Br>
<textarea style="width:500;height:120">
<?=$radio?>
</textarea>
<Br><Br>
默认(选中补选任何一个"radio"):
<?=$radio1?><Br>
<textarea style="width:500;height:120">
<?=$radio1?>
</textarea>
<Br><Br>
修改:
<?=$radio2?><Br>
<textarea style="width:500;height:120">
<?=$radio2?>
</textarea>
<Br><Br>
除VALUE值为"网络编程"的全部调用:
<?=$radio3?><Br>
<textarea style="width:500;height:120">
<?=$radio3?>
</textarea>
<br><br>
除KEY值为"54_title"全部调用:
<?=$radio4?><Br>
<textarea style="width:500;height:120">
<?=$radio4?>
</textarea>

作者: 特蓝克斯   发布时间: 2007-09-15

引用:
<?php
/*
* @copyright Copyright (c) 2007 ASEN (bbs.54php.com)
* @author  特蓝克斯
* @package Function
* @date    2007-09-11
* @return  Array
* @Notes   处理表单 Checkbox 表单控件
*/
include_once "helpers_forms.php";


$arr_checkBox = array(
//                        'key'        => 'value',
                        '54_name'    => "54master",
                        '54_title'    => "网络编程",
                        '54_group'    => "本版讨论群: 19870468"
);
$arr_values = array(
//                        'key'        => 'value',
                        '54_title'    => "网络编程",
                        '54_group'    => "本版讨论群: 19870468"
);
$Checkbox  = getCheckboxs($arr_checkBox);
$Checkbox1 = getCheckboxs($arr_checkBox, 'checkbox1', 'no');
$Checkbox2 = getCheckboxs($arr_checkBox, 'checkbox2', 'no' , $arr_values);
$Checkbox3 = getCheckboxs($arr_checkBox, 'checkbox3', 'no' ,'54_title');
$Checkbox4 = getCheckboxs($arr_checkBox, 'checkbox4', 'no' , '' , '', '', '', '', '' ,'54_title');
$Checkbox5 = getCheckboxs($arr_checkBox, 'checkbox5', 'no' , '' , '网络编程');
?>
默认(选中第一个"checkbox"):
<?=$Checkbox?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox?>
</textarea>
<Br><br>

一个不选择
<?=$Checkbox1?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox1?>
</textarea>
<Br><br>

通过数组构造选中多个Checkbox
<?=$Checkbox2?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox2?>
</textarea>
<Br><br>

选中其中一个Checkbox元素
<?=$Checkbox3?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox3?>
</textarea>
<Br><br>

除KEY值为"54_title"全部调用:
<?=$Checkbox4?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox4?>
</textarea>
<Br><Br>

除VALUE值为"网络编程"的全部调用:
<?=$Checkbox5?><Br>
<textarea style="width:500;height:120">
<?=$Checkbox5?>
</textarea>

作者: 特蓝克斯   发布时间: 2007-09-15

谢谢,

作者: dgh73   发布时间: 2007-09-15

:lol :lol :lol :lol :lol :lol :lol

作者: 疯子-斯   发布时间: 2007-10-15

逛逛

作者: chemila65   发布时间: 2007-10-16

付费

作者: chemila65   发布时间: 2007-10-16

的反复打

作者: chemila65   发布时间: 2007-10-16

引用:
原帖由 chemila65 于 2007-10-16 10:27 发表
逛逛
怎么引用的?

作者: chemila65   发布时间: 2007-10-16

引用:
原帖由 chemila65 于 2007-10-16 11:56 发表


怎么引用的?
嵌套呢?

作者: chemila65   发布时间: 2007-10-16

记号啦

作者: chouyu   发布时间: 2007-10-18

引用:
原帖由 chemila65 于 2007-10-16 11:57 发表


嵌套呢?
构造有规则的数组结构即可.
上面提供的几个例子,看一下,很容易理解.

作者: 特蓝克斯   发布时间: 2007-10-29

不错,朋友的帖子要顶!

作者: niohe   发布时间: 2007-10-29

欢迎多发原创精品文章。
:)

作者: luzhou   发布时间: 2007-10-30

作者: anysun   发布时间: 2007-11-01

555555555

作者: leesunny   发布时间: 2007-11-02



好东东。谢谢分享!

作者: 绿竹居   发布时间: 2007-11-02