转到会话中的上一页

时间:2014-10-08 00:11:00

标签: php

我正在完成一项学校作业,我必须翻译单词。在一个单独的页面上,我必须显示一个单词是对还是错,并且需要一个按钮才能返回上一页,这样您就可以继续使用下一个单词。

但是如何在不丢失会话的情况下返回上一页?

<a href="window.history.go(-1);">Previous page</a>

给我&#39;找不到对象&#39;。我和Xampp一起工作。

这是我的代码:

<?php
session_start();

$dutch = array('boom', 'kaas', 'hond', 'huis', 'auto');
$english = array('tree', 'cheese', 'dog', 'house', 'car');

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
  $_SESSION['counter'] = 0;
  $_SESSION['correct'] = 0;
  $_SESSION['incorrect'] = 0;
}
?>

<html>    
<body>
<p>Translate the following word: <strong><?php echo ucfirst($dutch[$_SESSION['counter']]); ?></strong></p>
<form action="opdracht3.11b.php" method="post">
<input type="text" name="answer" />
<input type="submit" value="Check!" />
<p>Progress: <?php echo $_SESSION['counter']."/".count($dutch); ?><br>
Correct words: <?php echo $_SESSION['correct']; ?><br>
Incorrect words: <?php echo $_SESSION['incorrect']; ?></p>
</form>
</body>
</html>

第二页的代码:

<?php
session_start();

$dutch = array('boom', 'kaas', 'hond', 'huis', 'auto');
$english = array('tree', 'cheese', 'dog', 'house', 'car');

if($_POST['answer'] == ($english[$_SESSION['counter']]))
    {
       $_SESSION['correct'] = $_SESSION['correct'] + 1;
       $_SESSION['counter'] = $_SESSION['counter'] + 1;
       echo "Correct!";
    }
    else
    {
      $_SESSION['incorrect'] = $_SESSION['incorrect'] + 1;
      $_SESSION['counter'] = $_SESSION['counter'] + 1;
       echo "Incorrect!";
    }
?>

<html>
<?php echo "<br/>";?>
<a href="window.history.go(-1);">Previous page</a>
</html>

编辑I: print_r($ _ SESSION)给了我:

    Array
(
    [form] => Array
        (
            [textfield1] => 1
            [textfield2] => 2
            [textfield3] => 3
            [submit] => Verzenden
        )

    [cijfers] => Array
        (
        )

    [counter] => 0
    [teller] => 0
    [goed] => 0
    [fout] => 0
    [back] => /opdracht3.11b.php
    [correct] => 0
    [incorrect] => 0
)

在发布之前,我改变了出纳员的位置,进行了纠正和纠正错误,所以不要介意。当我输入正确的翻译并在浏览器中使用上一页按钮时,会发生这种情况:

Array
(
    [form] => Array
        (
            [textfield1] => 1
            [textfield2] => 2
            [textfield3] => 3
            [submit] => Verzenden
        )

    [cijfers] => Array
        (
        )

    [counter] => 1
    [teller] => 0
    [goed] => 0
    [fout] => 0
    [back] => /opdracht3.11b.php
    [correct] => 1
    [incorrect] => 0
)

编辑II:

我改变了:

// Change #1
if($_SERVER['REQUEST_METHOD'] == 'GET')

// To:
if($_SERVER['REQUEST_METHOD'] == 'POST')

// Change #2
<html>
<?php echo "<br/>";?>
<a href="window.history.go(-1);">Previous page</a>
</html>

// To:
echo "<p><a href=\"javascript:history.go(-1)\" title=\"Return to previous page\">Go back</a></p>";

它现在有点起作用,至少会话确实如此。对于计数器,正确和不正确我得到未定义的索引错误。我想我需要使用isset()来摆脱这些错误..?

2 个答案:

答案 0 :(得分:0)

您是否尝试使用HTTP_REFERER替换它:

<?php echo "<br/>";?>
<a href="window.history.go(-1);">Previous page</a>
</html>

有了这个吗?

<html>
<?php echo "<br/>";?>
<a href=<?php echo '"'.$_SERVER['HTTP_REFERER'].'"';?>>Previous page</a>
</html>

答案 1 :(得分:0)

你试过这个吗?

window.history.back();

请试试这个..

http://www.w3schools.com/jsref/met_his_back.asp