我无法让提交按钮工作。我对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>
不完整的代码。你在哪里定义了数组'$ q_and_ans'。发布该代码。 – rmalviya
$ 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