我无法使提交按钮生效。我对php非常陌生,仅仅几个星期。目前,目标只是让表单提交到同一页面,以便用户可以看到他/她点击的内容。我正在使用多维数组和foreach循环来显示问题和选择。只是不能得到任何提交工作。任何提示或提示将不胜感激。
<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 :(得分: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>';
}
?>
答案 1 :(得分: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");
} .....