if(isset($_POST['next']))不工作,我似乎无法让它在任何函数中工作,而且我也无法弄


The if(isset($_POST['next'])) isn't working, I can't seem to get it to work in any function, and I can't figure out why for the life of me

我真的很难弄清楚为什么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";
    }
}

检查变量是否存在(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'])) {
// ....
}
相关文章: