1

我正在做一个在数据库中显示问题和答案的表单,现在我需要保存用户标记的答案。

我需要每个 RadioButton 的名称或选择下拉菜单。问题:我结合了 php 和 html,但我不知道如何传递单选按钮的名称或选择语法正确的下拉名称。单选按钮的答案名称是它们的 ID 以显示所有这些(RADIO17、Radio18、RADIO19、RADIOX...),现在我需要保存用户检查的单选按钮的特定 ID,例如 RADIO19。请帮忙。

        <!--RADIO BUTTON--> 

                <div>
                    <label class="desc"name="question" value=" <?php $row_questionset['QuestionIDFKPK'];?>">
                    <?php echo $row_questionset['QuestionValue']; ?>

                    </label>
                </div>
                    <?php while ($row_Answer=mysql_fetch_array($AnswersValue)){ ?>  
                    <fieldset class="radios">

                        <label class="label_radio">


                        **<input name="RADIO<?= $row_Answer['AnswerIDPK'] ?>" value="  <?= $ValueIDradio=$row_Answer['AnswerIDPK'] ?>" type="radio"  />**
                        <?php echo $row_Answer['AnswerValue']; ?> 
                        </label>


                    <?php }  ?>
                    </fieldset>


        <!--INSERTING ANSWERS-->
                                <?php

                                    $name=$_POST['RADIO1']; 

                                if(isset($_POST['submit'])){



                    ??????? -> $name=$_POST['RADIO$row_Answer['AnswerIDPK']'];  

                                $query_AnswerSelected="SELECT * FROM tblanswer WHERE tblanswer.AnswerIDPK = '".$name."' ";
                                $AnswersValueSelected= mysql_query($query_AnswerSelected);                                                                      
                                $row_AnswersValueSelected=mysql_fetch_array($AnswersValueSelected);
                                $Avalue= $row_AnswersValueSelected['AnswerValue'];
                                $useranswer= "INSERT INTO `nuevaspruebas`.`tbluseranswer` (`UserIDFKPK`, `AnswerIDFKPK`, `QuestionIDFK`, `AnswerValue`) VALUES ('$UserId','$name', '$QuestionID',  '$Avalue')";
                                mysql_query($useranswer);   
                                <?= $nameradio=$row_Answer['AnswerIDPK'] ?>;
                                }
                                    ?>
                                <!--INSERTING ANSWERS-->


            <!--RADIO BUTTON--> 


<?php } ?>
4

2 回答 2

1

对于答案,您必须在名称 attrbuite 中传递问题 ID,并且答案的值必须在值 attr 中传递

运行此代码以获得想法:

<?php
    if($_POST['submit']){
        #you can loop through answers using
        foreach($_POST['question'] as $quesID => $ansID){
            //action goes here
        }
        #view all the posted data
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
    }
?>
<?php
    $FirstQuestionID = 1;
    $SecondQuestionID = 2;
?>
<form method=post action=<?=$_SERVER['PHP_SELF'];?> >
    first question
    <br />
    <input type="radio" name="question['<?=$FirstQuestionID;?>']" value='1' />
    <input type="radio" name="question['<?=$FirstQuestionID;?>']" value='2' />
    <input type="radio" name="question['<?=$FirstQuestionID;?>']" value='3' />
    <input type="radio" name="question['<?=$FirstQuestionID;?>']" value='4' />
    <br />
    second question
    <br />
    <input type="radio" name="question['<?=$SecondQuestionID;?>']" value='1' />
    <input type="radio" name="question['<?=$SecondQuestionID;?>']" value='2' />
    <input type="radio" name="question['<?=$SecondQuestionID;?>']" value='3' />
    <input type="radio" name="question['<?=$SecondQuestionID;?>']" value='4' />
    <br />
    <input type=submit name=submit value=submit />
</form>
于 2012-11-12T10:12:41.670 回答
0

如果您有相互排斥的单选按钮,则所有单选按钮都必须使用相同的名称。从这个意义上说,您命名 RADIO1、RADIO19、RADIOxx 等的策略会给您带来问题,因为您的所有无线电都可以检查(不是唯一的)。

尝试类似:

<input name="radioName" value="<?= $ValueIDradio=$row_Answer['AnswerIDPK'] ?>" type="radio" />

插入答案时,您可以知道此处检查的收音机:

$radioChecked = $_REQUEST['radioName'];

于 2012-11-12T09:52:09.133 回答