我正在做一项学校作业,我必须翻译单词。在一个单独的页面上,我必须显示一个单词是对是错,需要有一个按钮返回上一页,这样你就可以继续下一个单词。
但是,如何在不丢失会话的情况下返回到上一页?
<a href="window.history.go(-1);">Previous page</a>
给我"找不到对象"。我正在与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="answerswer" />
<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
)
第二版:
我改了:
// 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()来消除这些错误。。?
您是否尝试通过替换以下内容来使用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>
你试过这个吗
window.history.back();
请这样试试。。
http://www.w3schools.com/jsref/met_his_back.asp