日期:2014-05-17  浏览次数:20490 次

php处理复选框checkbox
php在得到checkbox的值时和asp有稍有不同,他得把表单多选框的名子命名成类似php中的数组形式: name[],如<input name="area[]" type="checkbox" id="area[]" value="河北" />,要想得到checkbox的value数据关键也在于此。
例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php处理复选框checkbox实例</title>
</head>
<body>
<?php
    $area_arr = array();
    if($_GET['action'] == 'submit'){
        $area_arr = $_POST['area'];
        echo "您选定的地区为:&nbsp;";
        foreach ($area_arr as $key=>$v){
            echo $v."&nbsp;";
        }
    }
?>
<form id="form1" name="form1" method="post" action="?action=submit">
    <p><label>河北<input name="area[]" type="checkbox" id="area" value="河北" /></label></p>
    <p><label>河南<input name="area[]" type="checkbox" id="area[]" value="河南" /></label></p>
    <p><label>山西<input name="area[]" type="checkbox" id="area[]" value="山西" /></label></p>
    <p><label>山东<input name="area[]" type="checkbox" id="area[]" value="山东" /></label></p>
    <p><label>江苏<input name="area[]" type="checkbox" id="area[]" value="江苏" /></label></p>
    <p><label>浙江<input name="area[]" type="checkbox" id="area[]" value="浙江" /></label></p>
    <p><label><input type="submit" name="Submit" value="提交" /></label></p>
</form>
</body>
</html>