dongruoqiong9017 2016-10-30 11:25
浏览 52
已采纳

如何在PHP中设置单选按钮的值?

How do I set names for each radio button in the following PHP code using counter variable, or any other alternative?

<?php  

        $id=0; //counter variable 

        $sql ="select roll_number,student_name,gender from student_table";
        $query=mysqli_query($dbcon, $sql) or die(mysqli_error($dbcon));

          while($post=mysqli_fetch_assoc($query)){

             echo '<tr>';
            echo '<td>'. $post['roll_number'] . '</td>';
            echo '<td>'. $post['student_name'] . '</td>';
            echo '<td>'. $post['gender'] . '</td>';
            echo '<td width=250>';

            echo '<input type="radio" name="rad'.$id'" value="P" />Present';
            echo '<input type="radio" name="rad'.$id'" value="A" />Absent';

            echo '<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>';
            ++$id;
            echo '</td>';
            echo '</tr>';               

            }

           ?>
  • 写回答

2条回答 默认 最新

  • duanliang9288 2016-10-30 11:48
    关注
    echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="P" />Present';
    echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="A" />Absent';
    

    On the receiving end you may do like this:

    $answers = $_POST['rad'];
    foreach($answers as $roll_number=>$answer){
        //do something with the $roll_number and $answer...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员