动态单选按钮,不同的名称不同

时间:2012-11-12 09:40:43

标签: php html mysql forms radio-button

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

我需要每个RadioButton的名称或选择下拉列表。问题:我结合php和html,我不知道如何传递radiobutton的名称或选择下拉的名称语法正确。 radiobuttons答案名称是他们的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 } ?>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

尝试类似:

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

插入答案时,您可以知道此处检查的电台:

$ radioChecked = $ _REQUEST ['radioName'];