dtwvr26066 2012-08-20 13:59
浏览 18
已采纳

PHP复选框的问题

I'm having trouble understanding the logic behind a survey style script i'm working on.

I've written the first part where I pull the question and answer from the db but I can't figure out how I can create a multiple choice for each question whilst in the while loop and then store the value the user has selected. My brain is fried trying to figure it out :(

first part of my code is straight forward I think:

<?php
//retreive questions from database and put into question box

$query = "SELECT `Question`, `Answer` FROM `questions`";

$question = mysql_query($query);

while($row = mysql_fetch_assoc($question)){
?>
<div id="ContainerQuestion">
    <span style="Question">Question <?php echo $row['Question']; ?></span>
// Have A,B,C,D outputted as values in a checkbox and then the text after????
</div>
<?php
}

?> 

I'd really appreciate any help.

  • 写回答

2条回答 默认 最新

  • dongyao2001 2012-08-20 14:02
    关注

    Do you mean this?

    while($row = mysql_fetch_assoc($question)){
    ?>
    <div name="ContainerQuestion">
        <span style="Question">Question <?php echo $row['Question']; ?></span><br />
        <input type="checkbox" name="question_<?= $row['Question_ID']; ?>[]" value="A" <?= $row['Answer'] == 'A' ? 'checked="checked"' : '' ?> /> A<br />
        <input type="checkbox" name="question_<?= $row['Question_ID']; ?>[]" value="B" <?= $row['Answer'] == 'B' ? 'checked="checked"' : '' ?>/> B<br />
        <input type="checkbox" name="question_<?= $row['Question_ID']; ?>[]" value="C" <?= $row['Answer'] == 'C' ? 'checked="checked"' : '' ?>/> C<br />
        <input type="checkbox" name="question_<?= $row['Question_ID']; ?>[]" value="D" <?= $row['Answer'] == 'D' ? 'checked="checked"' : '' ?>/> D<br />
    // Have A,B,C,D outputted as values in a checkbox and then the text after????
    </div>
    <?php
    }
    

    NOTE: You shouldn't give the <div> a static (read: non-unique) ID within the loop - HTML rules state that IDs must be unique.

    Also, if the user may only select one option, you may want to use radio buttons instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制