2017-02-26 67 views
1

我无法让提交按钮工作。我对php很陌生,仅仅几个星期。目前,我们的目标只是让表单提交到同一页面,以便用户可以看到他/她点击了什么。我使用多维数组和foreach循环来显示问题和选择。只是不能得到任何提交工作。任何提示或技巧将不胜感激。php单选按钮提交MD阵列

<form method="post" action=""> 

    <?php 
     foreach($q_and_ans as $i => $q_and_an):?> 

      <p><?php echo $q_and_an['question'];?></p> 
      <?php foreach($q_and_an['ans'] as $a => $ans): ?> 
      <input type="radio" value="<?=$a?>" name="question[<?=$i?>]" > 
      <?php echo $ans;?><br> 
      <?php endforeach;?> 
    <?php endforeach;?> 

    <input type="submit" name="submit" id="submit" value="Submit"> 
</form> 
<?php 
if(isset($_POST['submit'])){ 

if (isset($_POST['question[0]'])){ 
$yourchoice0 = $POST['question[0]']; 
echo ("You selected ".$yourchoice0); 
} 
else{ 
    echo ("select an option"); 
} 

if (isset($_POST['question[1]'])){ 
$yourchoice1 = $POST['question[1]']; 
echo ("You selected ".$yourchoice1); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[2]'])){ 
$yourchoice2 = $POST['question[2]']; 
echo ("You selected ".$yourchoice2); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[3]'])){ 
$yourchoice3 = $POST['question[3]']; 
echo ("You selected ".$yourchoice3); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[4]'])){ 
$yourchoice4 = $POST['question[4]']; 
echo ("You selected ".$yourchoice4); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[5]'])){ 
$yourchoice5 = $POST['question[5]']; 
echo ("You selected ".$yourchoice5); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[6]'])){ 
$yourchoice6 = $POST['question[6]']; 
echo ("You selected ".$yourchoice6); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[7]'])){ 
$yourchoice7 = $POST['question[7]']; 
echo ("You selected ".$yourchoice7); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[8]'])){ 
$yourchoice8 = $POST['question[8]']; 
echo ("You selected ".$yourchoice8); 
} 
else{ 
echo ("select an option"); 
} 

if (isset($_POST['question[9]'])){ 
$yourchoice9 = $POST['question[9]']; 
echo ("You selected ".$yourchoice9); 
} 
else{ 
echo ("select an option"); 
} 
} 
    ?> 

</body> 
+0

不完整的代码。你在哪里定义了数组'$ q_and_ans'。发布该代码。 – rmalviya

+0

$ q_and_ans =阵列( \t阵列( '问题'=>'什么\的解说员叫什么名字? ' \t \t '答'=>阵列( \t \t \t '格雷格·施密茨', \t \t \t'格雷格·桑德斯, \t \t \t“格雷格 - 史密斯), \t \t \t 'correct_ans'=> 0), \t阵列( '问题'=> '什么是格雷格\' 新朋友\ '的名字?' , \t \t 'ANS'=>数组( \t \t \t '圣何塞', \t \t \t '曼努埃尔', \t \t \t 'Manuelo'), \t \t \t 'correct_ans'=> 1), \t阵列(”问题 '=> '是他们喝什么啤酒?', \t \t '答'=>阵列( \t \t \t '太平洋', \t \t \t' 科伦一个”, \t \t \t '比尔森'), \t \t \t 'correct_ans'=> 2), 等。这正好为 – theBartender

回答

1

这应该是你的问题更好的解决方案:

<?php 
// Sample Data, Replace with Yours 
$mcq = array(
    array('q' => 'question1?', 'opts' => ['opt1', 'opt2', 'opt3', 'opt4'], 'ans' => 0), 
    array('q' => 'question2?', 'opts' => ['opt1', 'opt2', 'opt3', 'opt4'], 'ans' => 1), 
    array('q' => 'question3?', 'opts' => ['opt1', 'opt2', 'opt3', 'opt4'], 'ans' => 2), 
    array('q' => 'question4?', 'opts' => ['opt1', 'opt2', 'opt3', 'opt4'], 'ans' => 3)); 
?> 
<form method="post" action=""> 
    <?php 
    $q_count = 0; 
    foreach($mcq as $q) { 
     $opt_count = 0; 
    ?> 
     <p> 
     <?php echo 'Q.'.($q_count + 1).' '.$q['q'].'<br />'; 
     foreach($q['opts'] as $opt) { 
     ?> 
      <input type="radio" id="<?php echo 'opt-'.$opt_count; ?>" value="<?php echo $opt; ?>" 
       name="<?php echo 'q-'.$q_count; ?>"> 
      <label for="<?php echo 'opt-'.$opt_count; ?>"><?php echo $opt; ?></label><br /> 
    <?php 
      $opt_count++; 
     } 
     unset($opt); 
     $q_count++; 
    } 
    unset($q); 
    ?> 
    </p> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
if(isset($_POST['submit'])) { 
    echo '<p>Your answers are:<br />'; 
    $count = 0; 
    foreach($mcq as $q) { 
     echo 'Q.'.($count + 1).':: Your answer: '; 
     if(isset($_POST['q-'.$count])) 
      echo $_POST['q-'.$count]; 
     else 
      echo 'Not Selected'; 
     echo ', Correct Answer: '.$q['opts'][$q['ans']].'<br />'; 
     $count++; 
    } 
    unset($q); 
    echo '</p>'; 
} 
?> 
+0

谢谢rmalviya!这是更清洁,更容易处理。你摇滚! – theBartender

+1

如果我的回答对你有帮助,请注意。 – rmalviya

0

尝试如下更改代码:

if (isset($_POST["question"][0])){ //<------------ change here 
$yourchoice0 = $_POST["question"][0];//<------------ change here 
echo ("You selected ".$yourchoice0); 
} 
else{ 
    echo ("select an option"); 
} 

if (isset($_POST["question"][1])){ 
$yourchoice1 = $_POST['question'][1]; 
echo ("You selected ".$yourchoice1); 
} 
else{ 
echo ("select an option"); 
} ..... 
+0

感谢另一八个问题! 现在我可以尝试弄清楚这个darn测验网站的其余部分。 – theBartender