0

我真的很难弄清楚为什么if(isset($_POST['next']))不起作用。我怎样才能在函数中调用类似的东西?

function questions($result){
    $options = array('A','B','C','D','E');
    echo "Please click 'Next'";
    echo "<p class = 'button'><input type='submit' name='next' value='Next'/></p>";

    while($row = $result->fetch_assoc()){
        if(isset($_POST['next'])) {
            echo "<h4>".$row['question']."</h4>";
            foreach($options as $option){
                $text = $row[$option];
                if($text)
                    echo "<br/><input type = 'radio' name = 'ans' value = '$option'>".$text."<br/>";
            }
            echo "<p class = 'button'><input type='submit' id='submit' class = 'answer' name='submitAns' value='Submit' /></p>";
            if(isset($_POST['submitAns'])){
                $answer = $row['answer'];
                if($_POST('ans') == $answer) { 
                    echo "<h4>Correct!</h4>";
                } else {
                    echo "<h4>Incorrect. Correct answer is: ".$answer."</h4>";
                }
                echo "<p class = 'button'><input type='submit' id='next' name='next' class = 'answer' value='Next' /></p>";
            }
        }
    }
    if(isset($_POST['next'])){
        echo "End of questions for this exam";
    }
}
4

1 回答 1

0

检查变量是否存在(isset)和检查变量是否实际包含某些东西(empty)是两个不同的东西。尝试这个:

function questions($result){
    $options = array('A','B','C','D','E');
    echo "Please click 'Next'";
    echo "<p class = 'button'><input type='submit' name='next' value='Next'/></p>";

    while($row = $result->fetch_assoc()){
        if($_POST && isset($_POST['next']) && !empty($_POST['next'])) {
// ....
}
于 2013-05-01T22:14:29.693 回答