A
A
Andrey Romanov2015-11-01 16:46:47
PHP
Andrey Romanov, 2015-11-01 16:46:47

How to display correct information from a text file?

Hello! I'm new to PHP and I'm having this kind of problem. There are two files, main.html and test.php. Main.html collects information about the user, test.php should display questions that are in a text file, but instead of them, an empty window. How to fix the problem?
Text file of the form:
Question 1 2+2 = ?
a)
b)
c)
d)
Question 2.
Code:
Main.html

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Страница регистрации</title>
    <h3 align = "center">Добро пожаловать!</h3>
</head>
<body>
<form accept-charset="UTF-8" action="test.php" method = "POST">
    <table border = "1" align = "center">
       <tr><td>Фамилия</td><td><input type = "text" name = "surname"></td></tr>
       <tr><td>Имя</td><td><input type = "text" name = "name"></td></tr>
        <tr><td>Пол</td><td><input type = "radio" name = "sex" value = "m" >М
            <input type = "radio" name = "sex" value = "f" >Ж</td></tr>
        <tr><td>Цифра класса</td>
           <td>
               <select name = "class">
                   <option value = "8">8</option>
                   <option value = "9">9</option>
                   <option value = "10">10</option>
                   <option value = "11">11</option>
               </select>
           </td>
        </tr>
    </table>
    <input align="center"  type = "submit" value = "OK">
</form>
</body>
</html>

test.php
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset = "UTF - 8">
    <title>Тест</title>
</head>
<body>

<?php
  echo("<p> Отвечает".$_POST["surname"]." ".$_POST["name"]." из ".$_POST["class"]." класса. </p>");
  $my_file = fopen('questions.txt','r');
  $n = fgets($my_file);

  echo("<form accept-charset=\"UTF-8\" action = \"answers.php\" method = \"post\">");

  for($i =1; $i < $n; $i++){
      $question = fgets($my_file);

      $answer_a = fgets($my_file);
      $answer_b = fgets($my_file);
      $answer_c = fgets($my_file);
      $answer_d = fgets($my_file);

      echo("<p><b></b>".$question."</b></p>");
      echo("<p><input type = \"radio\" name = \"answer_a\" value = \"a\" > ".htmlspecialchars($answer_a)."</p>");
      echo("<p><input type = \"radio\" name = \"answer_b\" value = \"a\" > ".htmlspecialchars($answer_b)."</p>");
      echo("<p><input type = \"radio\" name = \"answer_c\" value = \"a\" > ".htmlspecialchars($answer_c)."</p>");
      echo("<p><input type = \"radio\" name = \"answer_d\" value = \"a\" > ".htmlspecialchars($answer_d)."</p>");
      echo("<input type = \"submit\" value = \"Сохранить\">");
  }
  fclose($my_file);
echo("</form>");
?>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question