zoukankan      html  css  js  c++  java
  • 初学PHP:用post传递checkbox

    书本的做法:

    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>

    vinson
  • 相关阅读:
    PAT 乙级 1041 考试座位号(15) C++版
    四、Shell输入、输出功能和字符颜色设置
    三、Shell变量类型和运算符
    Shell文件权限和脚本执行
    Spark Standalone
    Loadrunner安装
    kali 2.0源更新
    xmanager远程桌面连接Linux
    Linux--文件查找命令
    Linux下MySQL忘记密码
  • 原文地址:https://www.cnblogs.com/vinsonLu/p/3040738.html
Copyright © 2011-2022 走看看