[Solved] How to fix an array of inputs and display it correctly


The problem I had was that there is 4 submit buttons each with different name tag so the session will only store the last input button and will make it functional but for the others it will only refresh the page with no different because they are not being controlled by php code so to solve this problem I only changed the EchoAnswers function like this:

function EchoAnswers($array) {
    shuffle($array);    
    echo "<form method='POST' action='' ";
    foreach ($array as $_SESSION['UserAnswer']) {
        echo "<button><input style="margin: 10px; border: 3px solid black; background-color: lightblue; width: 200px; height: 100px; font-size: 75px;" type="submit" name="UserAnswer" value="" . $_SESSION["UserAnswer'] . "' ></button";
    }
    echo "</form>";
}

the name tag was also set to $_SESSION['UserAnswer'] so I changed this part to a normal name and I also changed the php code like this:

if (isset($_POST['UserAnswer'])) {
    if ($_SESSION['QuestionNumber'] == 10) {
        header("location: result.php");
    }

    if ($_SESSION['UserAnswer'] == $_SESSION['CorrectAnswer']) {
        $_SESSION['QuestionNumber'] += 1;
        $_SESSION['CorrectAnswers'] += 1;
        $_SESSION['QuestionsAnswered'] += 1;
        QuestionGenerator();
    } else {
        $_SESSION['QuestionNumber'] += 1;
        $_SESSION['QuestionsAnswered'] += 1;
        QuestionGenerator();
    }
}

the part that I have changed in php code is isset($_POST['UserAnswer']) because UserAnswer was set to $_SESSION['UserAnswer'] instead of normal name in the name tag.

1

solved How to fix an array of inputs and display it correctly