ciberelfo
ciberelfo

Reputation: 183

PHP-How to get values from multiples dynamic radio button

i´m doing a little survey and i have the next problem. I get the data from database and the name of the answer is the id of the question(question 1 id=1, question 2 id=2...)

    <div id='box'>
       <form action='#' method='POST'>
           <?php
           foreach($lista as $list){
             echo "<p>".$list->getQuestion()."</p>";
          $idtemp=$list->getid(); //first get question
           $arr=$list->getQuestion($idtemp);
       foreach ($arr as $ar) { 
    //second, get and draw the radio with the answers per question
         echo "<input type='radio' value='".$ar[0]."' name='".$idtemp."'>".$ar[1]."</input><br>";

}

        }

           ?>

    <button type="button" >Cancel</button>

<button type='submit' value='end' name='end'>end</button>
<?php
if (isset($_POST['end'])) {
    echo "<span>selected :<b> ".$_POST['radio']."</b></span>";

    }
    ?>
    </form>
    </div> 

I only wants to get the radios checked and sending to database,

Upvotes: 0

Views: 1458

Answers (1)

ujash joshi
ujash joshi

Reputation: 307

Here, you need to use below line

echo "<input type='radio' value='".$ar[0]."' name='answer[".$temp."]'>".$ar[1]."</input><br>";

instead of this

echo "<input type='radio' value='".$ar[0]."' name='".$temp."'>".$ar[1]."</input><br>";

Then use

    <?php
    if (isset($_POST['answer'])) {
foreach($_POST['answer'] as $key=>$value){
        echo "<span>selected :<b> ".$key." ".$value."</b></span>";
    }
        }
        ?>

Upvotes: 3

Related Questions