Rgg
Rgg

Reputation: 45

Dynamic Radiobuttons, differents with unlimited names

I am doing a form that displays the questions and answers in a database and now I need to save the answers marked by the user.

I need a name for each RadioButton or select dropdown. PROBLEM: I combine php and html and i don't know how to pass the radiobutton's name or select drop down's name syntactically correct. The radiobuttons answers name are their ID to show all of them ( RADIO17, Radio18, RADIO19,RADIOX...), now I need to save the specific ID of the radiobutton checked by the user, for example RADIO19. Help please.

        <!--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 } ?>

Upvotes: 1

Views: 449

Answers (2)

Mohd Moe
Mohd Moe

Reputation: 577

for the answers you will have to pass the question id in the name attrbuite and the value of the answer must be passed in the value attr

Run this code to get the idea:

<?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>

Upvotes: 1

Hernan Velasquez
Hernan Velasquez

Reputation: 2820

If you have radio buttons that are mutually exclusive, all of the must be named with the same name. In that sense your strategy of naming RADIO1, RADIO19, RADIOxx, etc will bring you problems since all your radios can be checked (not one exclusively).

Try something like:

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

And when inserting the answer, you can know the radio that was checked here:

$radioChecked = $_REQUEST['radioName'];

Upvotes: 0

Related Questions