0

我做了一个while循环来获取数据库中的所有问题和答案,但是,我无法使用单选按钮为每个问题选择一个答案。

<form class ="formlayout" action = "Test.php" method = "post">
    <?php
    while ($row = mysqli_fetch_assoc($result)) {
        $question = $row['question'];
        $option1 = $row['option1'];
        $option2 = $row['option2'];
        $option3 = $row['option3'];
        ?>

        <?php echo $question ?> <br>
        <input type="radio" name="<?php echo $i ?>" value="$option1"><?php echo $option1 ?><br>
        <input type="radio" name="<?php echo $i ?>" value="$option2"><?php echo $option2 ?><br>
        <input type="radio" name="<?php echo $i ?>" value="$option3"><?php echo $option3 ?><br>
        <?php echo "" ?><br>
        <?php
    }
    ?>
        <input type="submit" value="Submit"/>
</form>

谢谢。

4

3 回答 3

0

我认为 $i 是未定义的,为什么不简单地使用一些静态字符串作为名称.. 例如:

<input type="radio" name="my_radio"/>
于 2013-05-31T07:26:18.863 回答
0

定义您的$i并使其具有不同的答案组,您也可以更改您的收音机名称,以便稍后轻松解析:

<form class ="formlayout" action = "Test.php" method = "post">
    <?php
    $i = 0;
    while ($row = mysqli_fetch_assoc($result)) {
        $question = $row['question'];
        $option1 = $row['option1'];
        $option2 = $row['option2'];
        $option3 = $row['option3'];
        ?>

        <?php echo $question ?> <br>
        <input type="radio" name="answer_<?php echo $i ?>" value="$option1"><?php echo $option1 ?><br>
        <input type="radio" name="answer_<?php echo $i ?>" value="$option2"><?php echo $option2 ?><br>
        <input type="radio" name="answer_<?php echo $i ?>" value="$option3"><?php echo $option3 ?><br>
        <?php echo "" ?><br>
        <?php
        $i++;
    }
    ?>
        <input type="submit" value="Submit"/>
</form>
于 2013-05-31T07:34:18.087 回答
0

只需给它们相同的名称:

<form class ="formlayout" action = "Test.php" method = "post">
    <?php
    while ($row = mysqli_fetch_assoc($result)) {
        $question = $row['question'];
        $option1 = $row['option1'];
        $option2 = $row['option2'];
        $option3 = $row['option3'];
        ?>

        <?php echo $question ?> <br>
        <input type="radio" name="YourName" value="$option1"><?php echo $option1 ?><br>
        <input type="radio" name="YourName" value="$option2"><?php echo $option2 ?><br>
        <input type="radio" name="YourName" value="$option3"><?php echo $option3 ?><br>
        <?php echo "" ?><br>
        <?php
    }
    ?>
        <input type="submit" value="Submit"/>
</form>
于 2013-05-31T07:07:44.883 回答