drt3751 2011-06-27 22:28
浏览 108
已采纳

如何使用php处理可变数量的复选框

My goal is to process an unknown amount of names (between 2 and 8) and format it in a nice way. The amount of names varies per user. So one user will see 4 names, another will see only 2 names. I'll show an example with 3 names.

<input type="checkbox" name="inuser[]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[]" id="u3" value="Lotte" /><label for="u3">Lotte</label>

Say Rosa's checkbox is checked. Than I want to store 'Rosa' in the table. Say Rosa and Floris are checked. Now I want to store 'Floris and Rosa' Say all three are checked. Than I want to store 'Floris, Rosa and Lotte'

I'm not that experienced with arrays. So this is an unsolvable problem for me. I've tried to solve it by reading tutorials, but I need someone to help me a bit.

Let me try to explain why the amount of names are variable... On the site students will be able to log there school project activities. They work in groups, but the amount of people differ. So every 'project' will have a different amount of students working on it.

When they log an activity the students who carried out the activity can be checked and the server should turn this into a string (for example: 'Floris, Rosa and Lotte') and store it in the log table. So later on the server will be able to retrieve the log information (What, When, Who, etc.)

Feel free to correct my terrible English. I'm from Holland.

  • 写回答

3条回答 默认 最新

  • drgwsx8405 2011-06-27 22:51
    关注

    Here's another code example that might help:

    <?php
    if(isset($_POST['inuser'])){
        foreach($_POST['inuser'] as $name){
            echo $name . "<br />";
        }
    }else{
        $array = array("Floris", "Rosa", "Lotte");
        $i = 1;
        echo '<form action="test.php" method="post" name="names">';
        foreach($array as $name){
            echo '<input type="checkbox" name="inuser[]" id="u' . $i . '" value="' . $name . '" /><label for="u' . $i . '">' . $name . '</label>';
            $i++;
        }
        echo '<input type="submit" name="Submit" value="Submit" /></form>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100