简体   繁体   English

如果选择或不选择单选按钮的不同值

[英]different values for radio button if selected or not

I'm working on a online exam website. 我正在网上考试网站上工作。

when I want to define a question with its choices, I should choose the correct choice. 当我想用它的选择来定义一个问题时,我应该选择正确的选择。 so there is a radio button in front of each choice that if I choose it, It will be the correct choice. 所以每个选项前面都有一个单选按钮,如果我选择它,它将是正确的选择。

I want when I choose one of the radio buttons, the value of it would be '1' and if not, the value would be '0', so I can save them in database with foreach. 我想当我选择其中一个单选按钮时,它的值将为“1”,如果不是,则值为“0”,因此我可以将它们保存在数据库中,并使用foreach。 that's my problem. 那是我的问题。

Since the number of choices is not clear (from 2 to more) and it can be added dynamically, I can't set specific values for them. 由于选项数量不明确(从2到更多)并且可以动态添加,我无法为它们设置特定值。

I used isset and foreach but I guess I didn't use them in correct way. 我使用了isset和foreach,但我想我没有以正确的方式使用它们。 I think it can be done by javascript or jquery but I don't know how. 我认为它可以通过javascript或jquery完成,但我不知道如何。

choice 1:
<textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
<label><input type="radio" name="correct[]">Correct</label>

choice 2:
<textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
<label><input type="radio" name="correct[]">Correct</label>

Other Choices ...



<?php
foreach ( $_POST['choice'] as $key => $value ){
if (!empty($value)){
    foreach ($_POST['correct'] as $value2){ 
    if (isset($value2)) $choice_answer = '1'; else $choice_answer = '0';}

mysqli_query($server,"INSERT INTO question_choices (question_id,exam_id,choice,answer) VALUES ('$question_id','$current_exam_id','$value','$choice_answer')");}}
?>

So you have your choices being stored in an array, this is good. 所以你把你的选择存储在一个数组中,这很好。 However, you also have your correct indicator being stored in an array. 但是,您还可以将正确的指示符存储在数组中。 You don't need it in an array since there is only one correct choice. 你不需要在数组中,因为只有一个正确的选择。

So change your correct[] to just correct and set the value incrementally for every choice. 因此,将correct[]更改为correct并为每个选项逐步设置值。

Ex: 例如:

choice 1:
<textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
<label><input type="radio" name="correct" value=0>Correct</label>

choice 2:
<textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
<label><input type="radio" name="correct" value=1>Correct</label>

choice 3:
<textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
<label><input type="radio" name="correct" value=2>Correct</label>

Since only selected radio buttons get posted, $_POST['correct'] will contain the index of the correct answer within the $_POST['choice'] array. 由于只有选定的单选按钮被发布, $_POST['correct']将包含$_POST['choice']数组中正确答案的索引。

Then you can do something like this to set your value accordingly before you send to the database: 然后,您可以执行以下操作,在发送到数据库之前相应地设置值:

foreach ($_POST['choice'] as $i => $choice) {
    if (!empty($choice)) {

        if($_POST['correct'] == $i){
            $choice_answer = 1;
        } else {
            $choice_answer = 0;
        }

        mysqli_query($server, "INSERT INTO question_choices (question_id,exam_id,choice,answer) VALUES ('$question_id','$current_exam_id','$value','$choice_answer')");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM