Help complete please.
This code is used to decode a json file from a directory and create a test from it. I need that when sending the form, the entered values ​​are recorded in an array and at the end the result is how many correct answers, how many are not correct. I, probably, did not set the correct format for the form, can it be easier to implement using selection markers, and send the results with one button?

<?php $test_dir = "./tests/test"; $test_id = $test_dir.$_GET["id"].".json"; $json_file = file_get_contents($test_id); $json_array = json_decode($json_file, true); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <nav> <ul> <li><a href="list.php">Список тестов</a></li> <li><a href="admin.php">Загрузить тест</a></li> </ul> </nav> <?php echo "<pre>"; var_dump($json_array); $result=[]; ?> <form action="" method="GET" enctype="multipart/form-data"> <h1><?php echo $json_array[0]['question'] ?></h1> <h2>Варианты ответов:</h2> <?php foreach ($json_array[0]['answers'] as $values) { ?> <p> <?php echo $values; ?> </p> <?php } ?> Напишите номер ответа: <input type="text" name="question1" value=" "><br> <input type="submit" value="Ответить"> </form> <form action="" method="GET" enctype="multipart/form-data"> <h1><?php echo $json_array[1]['question'] ?></h1> <h2>Варианты ответов:</h2> <?php foreach ($json_array[1]['answers'] as $values) { ?> <p> <?php echo $values; ?> </p> <?php } ?> Напишите номер ответа: <input type="text" name="question2" value=" "><br> <input type="submit" value="Ответить"> </form> <form action="" method="GET" enctype="multipart/form-data"> <h1><?php echo $json_array[2]['question'] ?></h1> <h2>Варианты ответов:</h2> <?php foreach ($json_array[2]['answers'] as $values) { ?> <p> <?php echo $values; ?> </p> <?php } ?> Напишите номер ответа: <input type="text" name="question3" value=" "><br> <input type="submit" value="Ответить"> </form> <?php $answer1 = $_GET['question1']; $answer2 = $_GET['question2']; $answer3 = $_GET['question3']; $result = [$answer1, $answer2, $answer3]; echo "<pre>"; var_dump($result); ?> </body> </html> 

Below is the structure of the json test.

  [{ "question": "Столица России?", "answers": { "1": "1. Париж", "2": "2. Вашингтон", "3": "3. Питер", "4": "4. Москва" }, "correct_answer": "4" }, { "question": "Какой сейчас год?", "answers": { "1": "1. 2025", "2": "2. 1991", "3": "3. 2017", "4": "4. 2013" }, "correct_answer": "3" }, { "question": "Сколько дней в январе?", "answers": { "1": "1. 31", "2": "2. 29", "3": "3. 30", "4": "4. 28" }, "correct_answer": "1" }] 
  • Why do you need submit after each question? - teran
  • @teran probably to write the result to the database or to another repository - DaemonHK

1 answer 1

You have every question is a form. When you click on the answer button, a form is sent out and the script is called accordingly. And each time, a "new copy" of the script is invoked which knows nothing about all past calls. In order to fill in the answers of users, response data must be recorded in a session, database or file.
For example, use the session. At the very beginning of the file, the first line after

 <?php if (isset($_GET['question1'])) { $_SESSION['answers'][1] = $_GET['question1']; } if (isset($_GET['question2'])) { $_SESSION['answers'][2] = $_GET['question1']; } if (isset($_GET['question3'])) { $_SESSION['answers'][3] = $_GET['question3']; } var_dump($_SESSION['answers']); ?> 

This is for example, of course you need to get answers in a loop by the number of questions in the json file. In the end, you end up with $ _SESSION ['answers'] which will have an array of user answers that you need to check for correctness and output the number.

  • Thank you for the detailed answer, but it is quite difficult for me to understand this. I, probably, are far from understanding of this and need additional. Information on this material, where to read, read, to be available, do not tell? - Sindr0me
  • In general, to start php.net/manual/ru/tutorial.php - ErroR