使用PHP会话记住用户在表单中的位置

时间:2012-05-31 18:20:41

标签: php forms session session-cookies

我做了一个包含4个是/否问题的简短测验,我试图做的是如果用户在第二个问题上关闭网页时,那么当他们重新打开时页面(最多30天后),他们将被带到他们留下的同一个问题。下面是我到目前为止的一些代码。

<?php
session_set_cookie_params(2592000); //Sets cookie to last for 30 days
session_start();
?>

这是问题的代码

//Variables for functions
 $number;
 $creature2;
 $creature3;
 //Function for showing questions 2 to 7
  function showquestion($number) {

      echo "<div class='questions'><form method ='GET' action='Creatures.php'>
     <input type='submit' name='answer$number' value='Yes' class='buttons' />
     <input type='submit' name='answer$number' value='No' class='buttons' />
     </form></div>";
 }

  //Function for showing questions 8 to 15
  function showanswer($creature2,$creature3) {
      echo "<div class='questions'><form method ='GET' action='Creatures.php'>
     <button type='submit' name='final' value='$creature2' class='buttons'> Yes </button>
     <button type='submit' name='final' value='$creature3' class='buttons'> No </button>
     </form></div>";
 }
//If start button has not been pressed, display nothing
if (!isset($_POST['start'])){

} //If start button has been pressed, display questions
else{ 
echo $firstquestion;
     echo "<div class='questions'><form method ='GET' action='Creatures.php'>
     <input type='submit' name='yes1' value='Yes' class='buttons' />
     <input type='submit' name='no1' value='No' class='buttons' />
     </form></div>";
}
//Question 2
if ($_GET['yes1']) //If answer to Q1 is yes then display this
{
echo "<div class='questions'><p>{$questions[0][0]}</p></div>";
showquestion(1);
}

//Question 3
if ($_GET['no1']) 
{
echo "<div class='questions'><p>{$questions[0][1]}</p></div>"; //If answer to Q1 is no then display this
showquestion(2);
}

//Questions 4 and 5
switch($_GET['answer1']) //If Question 2 is yes, do this
{
case 'Yes': echo "<div class='questions'><p>{$questions[1][0]}</p></div>";
showquestion(3);
break;
case 'No': echo "<div class='questions'><p>{$questions[1][1]}</p></div>";
showquestion(4);
}

//Questions 6 and 7
switch($_GET['answer2']) //If Question 3 is yes, do this
{
case 'Yes': echo "<div class='questions'><p>{$questions[1][2]}</p></div>";
showquestion(5);
break;
case 'No': echo "<div class='questions'><p>{$questions[1][3]}</p></div>";
showquestion(6);
}

//Questions 8 and 9
switch($_GET['answer3']) 
{
case 'Yes': echo "<div class='questions'><p>{$questions[2][0]}</p></div>";
showanswer('Eagle','Parrot');
break;
case 'No': echo "<div class='questions'><p>{$questions[2][1]}</p></div>";
showanswer('Ostrich','Turkey_(bird)');
}

//Questions 10 and 11
switch($_GET['answer4']) 
{
case 'Yes': echo "<div class='questions'><p>{$questions[2][2]}</p></div>";
showanswer('Grasshopper','Ant');
break;
case 'No': echo "<div class='questions'><p>{$questions[2][3]}</p></div>";
showanswer('Gorilla','Tiger');
}

//Questions 12 and 13
switch($_GET['answer5']) 
{
case 'Yes': echo "<div class='questions'><p>{$questions[3][0]}</p></div>";
showanswer('Penguin','Goose');
break;
case 'No': echo "<div class='questions'><p>{$questions[3][1]}</p></div>";
showanswer('Frog','Salamander');
}

//Questions 14 and 15
switch($_GET['answer6']) 
{
case 'Yes': echo "<div class='questions'><p>{$questions[3][2]}</p></div>";
showanswer('Octopus','Jellyfish');
break;
case 'No': echo "<div class='questions'><p>{$questions[3][3]}</p></div>";
showanswer('Goldfish','Eel');
}

我知道这是可能的,因为我以前见过其他人这样做。测验在这里My Quiz

0 个答案:

没有答案