Скрывайте вопрос викторины, пока на предыдущий не ответите, используя PHP - PullRequest
0 голосов
/ 18 апреля 2020

Итак, я сделал тест, используя html, и теперь мне нужно использовать скрипт php, чтобы сделать функцию викторины такой, чтобы был доступен только вопрос, и как только на этот вопрос ответили, появится следующий вопрос. Поэтому, когда пользователь запускает тест, появляется только первый вопрос, а затем, когда на него появляется ответ, появляется второй. А потом после последнего. Я включил свой html код внизу. И мой php скрипт. Сценарий php направлен в правильном направлении, если нет, что я делаю неправильно и как я могу это исправить?

<!DOCTYPE html>
<html>
<head>
  <title>Test your Math Skills!</title>
  <link href ="style.css" rel ="stylesheet">
</head>
<body>
      <div class="container">
        <div id="question-container" class="hide">
          <h1><center><u> Are You Smarter Than A Fifth Grader? 
 </center></h1></u>
        <img id = "image" src = "Photos/gameLogo.jpg" style = "width:400px;height:300px;">
          </div>

<form action="script.php" method="post">

<p class = "questions">What ocean is Japan apart of?</p>

<input type = "radio" name = "q1" value = "Arctic"> Arctic<br>
<input type = "radio" name = "q1" value = "Pacific"> Pacific<br>
<input type = "radio" name = "q1" value = "Atlantic"> Atlantic<br>
<input type = "radio" name = "q1" value = "Indian"> Indian<br>
<input type="submit" name="submit" />

<p id = "question2" style = display:none> How many feet are in a mile?<br>
<input type = "radio" name = "q2" value = "1280"> 1280<br>
<input type = "radio" name = "q2" value = "3280"> 3280<br>
<input type = "radio" name = "q2" value = "2280"> 2280<br>
<input type = "radio" name = "q2" value = "5280"> 5280<br> 
<input type="submit" name="submit" />

<p id = "question3" style = display:none> What is something that is always arriving but never comes? <br>
<input id = "textbox" type = "text" name = "q3">
<input type="submit" name="submit" />

</form>
</html>
</body>

Это сценарий:

 if ($_POST['submit'] )  
 {

   echo $question2;

  }

 ?>

Ответы [ 2 ]

0 голосов
/ 18 апреля 2020

Попробуйте этот подход:

Ваши ответы будут сохранены в сеансе 'choices' (var_dump ($ _ SESSION ['choices']))).

<?php @session_start();
    if(isset($_POST['clear'])){
        if(isset($_SESSION['choices'])){ unset($_SESSION['choices']); }
    }

    if(isset($_POST['submit'])){
        if(isset($_POST['q']) && trim($_POST['q'])!=""){
            $_SESSION['choices'][]=$_POST['q'];
            header("location:".$_SERVER['PHP_SELF']);
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
  <title>Test your Math Skills!</title>
  <link href ="style.css" rel ="stylesheet">
</head>
<body>
      <div class="container">
        <div id="question-container" class="hide">
          <h1><center><u> Are You Smarter Than A Fifth Grader? 
 </center></h1></u>
        <img id = "image" src = "Photos/gameLogo.jpg" style = "width:400px;height:300px;">
          </div>

<form action="script.php" method="post">
<?php if(count(@$_SESSION['choices'])==0){?>
    <p class = "questions">What ocean is Japan apart of?</p>
    <br/>
    <input type = "radio" name = "q" value = "Arctic"> Arctic<br>
    <input type = "radio" name = "q" value = "Pacific"> Pacific<br>
    <input type = "radio" name = "q" value = "Atlantic"> Atlantic<br>
    <input type = "radio" name = "q" value = "Indian"> Indian<br>
    <input type="submit" name="submit" />
<?php }elseif(count(@$_SESSION['choices'])==1){?>
    <p id = "question2"> How many feet are in a mile?</p>
    <br/>
    <input type = "radio" name = "q" value = "1280"> 1280<br>
    <input type = "radio" name = "q" value = "3280"> 3280<br>
    <input type = "radio" name = "q" value = "2280"> 2280<br>
    <input type = "radio" name = "q" value = "5280"> 5280<br> 
    <input type="submit" name="submit" />
<?php }elseif(count(@$_SESSION['choices'])==2){?>
    <p id = "question3"> What is something that is always arriving but never comes? </p>
    <br/>
    <input id = "textbox" type = "text" name = "q">
    <input type="submit" name="submit" />
<?php }else{?>
<br/>
<br/>
Result:
<hr/>
<?php foreach($_SESSION['choices'] as $key=>$choice){?>
    <div><?php echo $key+1; ?> = <?php echo $choice; ?> 
    <?php if(
        ($key==0 && $choice=="Pacific") 
//      || ($key==1 && $choice=="Q2 ANSWER") 
//      || ($key==2 && $choice=="Q3 ANSWER") 
    ){ echo "(Correct!)"; }else{ echo "(Wrong!)"; }?>
    </div>
<?php }?>
<br/>
<br/>
<?php } ?>
<br/>
<input type="submit" name="clear" value="Restart" />
</form>
</body>
</html>
0 голосов
/ 18 апреля 2020

Вы можете сделать это следующим образом:

script. php:

<?php
$show_q2 = false;
if(isset($_POST['submit']) && isset($_POST['q1'])){
    //Process question
    $show_q2 = true;
}

?>

<form action="script.php" method="post">
<?php if(!$show_q2):?>
   <p class = "questions">What ocean is Japan apart of?</p>

   <input type = "radio" name = "q1" value = "Arctic"> Arctic<br>
   <input type = "radio" name = "q1" value = "Pacific"> Pacific<br>
   <input type = "radio" name = "q1" value = "Atlantic"> Atlantic<br>
   <input type = "radio" name = "q1" value = "Indian"> Indian<br>
   <input type="submit" name="submit" />
<?php else: ?>
   <p id = "question2"> How many feet are in a mile?<br>
   <input type = "radio" name = "q2" value = "1280"> 1280<br>
   <input type = "radio" name = "q2" value = "3280"> 3280<br>
   <input type = "radio" name = "q2" value = "2280"> 2280<br>
   <input type = "radio" name = "q2" value = "5280"> 5280<br> 
   <input type="submit" name="submit" />
<?php endif; ?>
<p id = "question3" style = display:none> What is something that is always arriving but never comes? <br>
<input id = "textbox" type = "text" name = "q3">
<input type="submit" name="submit" />

</form>

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...