PHP-如何从倍数动态单选按钮获取值

时间:2016-12-05 10:13:03

标签: php

我正在进行一些调查,我有下一个问题。 我从数据库中获取数据,答案的名称是问题的ID(问题1 id = 1,问题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> 

我只想检查无线电并发送到数据库,

1 个答案:

答案 0 :(得分:2)

在这里,你需要使用下面的行

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

而不是

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

然后使用

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