【问题标题】:php radio buttons submission MD arrayphp单选按钮提交MD数组
【发布时间】:2017-02-26 05:28:48
【问题描述】:

我无法让提交按钮正常工作。我对 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>

【问题讨论】:

  • 代码不完整。你在哪里定义了数组$q_and_ans。也发布该代码。
  • $q_and_ans = array( array('question' => '旁白的名字是什么?', 'ans' => array('Greg Schmitz', 'Greg Sanders', 'Greg Smith'), 'correct_ans' => 0), array('question' => 'Greg 的新朋友叫什么名字?', 'ans' => array('Jose', 'Manuel', 'Manuelo'), 'correct_ans' => 1), array('question' => '他们喝什么啤酒?', 'ans' => array('Pacifico', 'Corona', 'Pilsener'), ' correct_ans' => 2) 等等。这又是另外八个问题

标签: php arrays forms


【解决方案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>';
}
?>

【讨论】:

  • 谢谢 rmalviya!这更清洁,更容易使用。你摇滚!
【解决方案2】:

尝试如下更改您的代码:

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");
} .....

【讨论】:

  • 谢谢!!!现在我可以尝试找出这个该死的测验网站的其余部分。
猜你喜欢
  • 2013-01-21
  • 2013-03-28
  • 2012-04-05
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2013-12-17
相关资源
最近更新 更多