书本的做法:

user_info.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>vinson firt php</title>
<meta name="Generator" content="">
<meta name="Author" content="czh">
<meta name="Keywords" content="php date">
<meta name="Description" content="">
</head>
<style>
.body

 font-size:10pt;
 coclor:#000000;
}
</style>
<body class='body'>
<table border=0 cellspacing='0' cellpadding='0' class='body'>
 <form name='php_test' method='post' action='./user_info.php'>
 <tr height='21px'>
     <td>
         name:<input type="text" name='user_name' size='15'>
     </td>
     <td>
         sex:
         <select name='user_sex' style="width=100px">
             <option value='0'>man</option>
             <option value='1'>woman</option>
         </select>
     </td>
 </tr>
 <tr height='21px'>
     <td>
         Sports hobby:
         <input type="checkbox" name='badminton' value="badminton">badminton
         <input type="checkbox" name='basketball' value="basketball">basketball
     </td>
     <td>
         <input type="checkbox" name='football' value="football">football
         <input type="checkbox" name='vollyball' value="vollyball">vollyball
     </td>
 </tr>
  <tr height='21px'>
     <td>
     </td>
     <td>
         <input type="submit" value="confirm">
     </td>
 </tr>
 </form>
</table>
</body>
</html>

  

user_info.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>vinson firt php</title>
<meta name="Generator" content="">
<meta name="Author" content="czh">
<meta name="Keywords" content="php date">
<meta name="Description" content="">
</head>
<style>
.body
 font-size:10pt;
 
 coclor:#000000;
}
</style>
<body class='body'>

<?php 
if($_POST)
    $name=$_POST['user_name'];
    if($_POST['user_sex']==0)
        $sex="man";
    else 
        $sex="woman";
    $user_insterest="you like:";
    if ($_POST['badminton']) 
        $user_insterest.=$_POST['badminton']."/";    
    if ($_POST['basketball'])
        $user_insterest.=$_POST['basketball']."/";
    if ($_POST['football'])
        $user_insterest.=$_POST['football']."/";
    if ($_POST['vollyball'])
        $user_insterest.=$_POST['vollyball']."/";
        
    $user_insterest=substr($user_insterest, 0,-1);
    echo "your name:".$name."<br>";
    echo "your sex:".$sex."<br>";
    echo $user_insterest;
?>
</body>
</html>

 

这样做,如果哪个checkbox没有选上就报哪个错

4个复选框checkbox的name应该一样,并且以数组形式命名:

user_info.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>vinson firt php</title>
<meta name="Generator" content="">
<meta name="Author" content="czh">
<meta name="Keywords" content="php date">
<meta name="Description" content="">
</head>
<style>
.body

 font-size:10pt;
 coclor:#000000;
}
</style>
<body class='body'>
<table border=0 cellspacing='0' cellpadding='0' class='body'>
 <form name='php_test' method='post' action='./user_info.php'>
 <tr height='21px'>
     <td>
         name:<input type="text" name='user_name' size='15'>
     </td>
     <td>
         sex:
         <select name='user_sex' style="width=100px">
             <option value='0'>man</option>
             <option value='1'>woman</option>
         </select>
     </td>
 </tr>
 <tr height='21px'>
     <td>
         Sports hobby:
         <input type="checkbox" name='Sportshobby[]' value="badminton">badminton
         <input type="checkbox" name='Sportshobby[]' value="basketball">basketball
     </td>
     <td>
         <input type="checkbox" name='Sportshobby[]' value="football">football
         <input type="checkbox" name='Sportshobby[]' value="vollyball">vollyball
     </td>
 </tr>
  <tr height='21px'>
     <td>
     </td>
     <td>
         <input type="submit" value="confirm">
     </td>
 </tr>
 </form>
</table>
</body>
</html>

  

user_info.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>vinson firt php</title>
<meta name="Generator" content="">
<meta name="Author" content="czh">
<meta name="Keywords" content="php date">
<meta name="Description" content="">
</head>
<style>
.body
 font-size:10pt;
 
 coclor:#000000;
}
</style>
<body class='body'>

<?php 
if($_POST)
    $name=$_POST['user_name'];
    if($_POST['user_sex']==0)
        $sex="man";
    else 
        $sex="woman";
    $user_insterest="you like:";
    if (isset($_POST['Sportshobby']))//判断元素是否存在
    {
        $v=$_POST['Sportshobby'];//這樣取到的是一個數組,4個checkbox的value
        foreach ($v as $value)//再循環取出
        {
            $user_insterest.=$value."/";    
        }
    }
    $user_insterest=substr($user_insterest, 0,-1);
    echo "your name:".$name."<br>";
    echo "your sex:".$sex."<br>";
    echo $user_insterest;
?>
</body>
</html>

 

 

posted on 2013-04-24 18:22  vinsonLu  阅读(4911)  评论(0编辑  收藏  举报