我有一个php文件和一个xml文件。 php应该将xml解析为一个测验,但每当我点击提交查询输入我的答案选项时,会话变量都不会延续。该页面应该刷新并向测验提出另一个问题。 这是正确用法吗?
<!DOCTYPE html>
<?php
session_start(); $_SESSION['qcorrect']; $_SESSION['quizno']; $_SESSION['qvar'] = 0;
if (!isset($_SESSION['quizno'])){
$_SESSION['quizno'] = 0;
}
if (!isset($_SESSION['qcorrect'])){
$_SESSION['qcorrect'] = 0;
}
?>
<html>
<head>
<style>
h1 { text-align:center; }
p { text-align:center; }
</style>
<title> Memequiz.net </title>
</head>
<body>
<h1>Memequiz.net</h1>
<div id = "image" style = width:"500px" align = "middle">
<?php
$xml = simplexml_load_file('firsquiz.xml');
$question = ($xml -> question[$_SESSION['quizno']] -> ask);
$correctans = ($xml -> question[$_SESSION['quizno']] -> correctanswer);
$answer0 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']] -> text);
$answer1 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+1] -> text);
$answer2 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+2] -> text);
$answer3 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+3] -> text);
echo "<img src = \"".$xml -> question[$_SESSION['quizno']] -> picture."\" alt = \"Meme\" width = \"400\" height = \"350\">";
echo "<p><br>Question ".$_SESSION['quizno']."<p>Number Correct: ".$_SESSION['qcorrect']."</p><p>".$question."<br></p>";
echo "<form action = \"\" method = \"get\">";
echo "<table>";
echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer0."\"></td> <td>".$answer0."</td> </tr>";
echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer1."\"></td> <td>".$answer1."</td> </tr>";
echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer2."\"></td> <td>".$answer2."</td> </tr>";
echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer3."\"></td> <td>".$answer3."</td> </tr>";
echo "</table>";
echo "<input type = \"submit\" name = \"submit1\">";
echo "<input type = \"submit\" name = \"refresh\">";
echo "</form>";
echo "You submitted: ";
echo "<br><br><br>";
echo "my xml file: ";
if (isset($_GET['submit1'])){
$_SESSION['quizno']++;
if ($correctans == $_GET['ans']){
echo "Correct Answer!";
$_SESSION['qcorrect']++;
}else{
echo "The wrong answer, try again!";
}
}
if (isset($_GET['refresh'])){
$_SESSION['qcorrect'] = 0;
$_SESSION['quizno'] = 0;
}
?>
</div>
</body>
</html>
XML file:
<?xml version = "1.0"?>
<quiz>
<question>
<ask>What is the name of the movie that this meme comes from?</ask>
<correctanswer>Downfall</correctanswer>
<picture>question1.jpg</picture>
<answers>
<answer>
<text>Stalingrad</text>
</answer>
<answer>
<text>Downfall</text>
</answer>
<answer>
<text>Band of Brothers</text>
</answer>
<answer>
<text>The Fuhrer</text>
</answer>
</answers>
</question>
<question>
<ask>What is the name of this actor?</ask>
<correctanswer>Bruno Ganz</correctanswer>
<picture>question1.jpg</picture>
<answers>
<answer>
<text>Bruno Ganz</text>
</answer>
<answer>
<text>Ulrich Matthes</text>
</answer>
<answer>
<text>Thomas Kretschmann</text>
</answer>
<answer>
<text>Hans Funck</text>
</answer>
</answers>
</question>
<question>
<ask>What is the name of the youtube account in which this first appeared in?</ask>
<picture>questionn2.jpg</picture>
<correctanswer>wzr0713</correctanswer>
<answers>
<answer>
<text>wzr0713</text>
</answer>
<answer>
<text>rerer434</text>
</answer>
<answer>
<text>karetoshare</text>
</answer>
<answer>
<text>blueberrypie573</text>
</answer>
</answers>
</question>
<question>
<ask>What date did this picture first appear on the internet?</ask>
<picture>questionn2.jpg</picture>
<correctanswer>June 6 2012</correctanswer>
<answers>
<answer>
<text>Aug 12 2011</text>
</answer>
<answer>
<text>Oct 22 2010</text>
</answer>
<answer>
<text>Aug 13 2011</text>
</answer>
<answer>
<text>June 6 2012</text>
</answer>
</answers>
</question>
<question>
<ask>This is a stock photo from which company?</ask>
<correctanswer>Corbis Images</correctanswer>
<picture>question3.png</picture>
<answers>
<answer>
<text>Foter</text>
</answer>
<answer>
<text>Getty Images</text>
</answer>
<answer>
<text>Sipa Press</text>
</answer>
<answer>
<text>Corbis Images</text>
</answer>
</answers>
</question>
<question>
<ask>Which website did this meme originate from?</ask>
<correctanswer>quickmeme</correctanswer>
<picture>question3.png</picture>
<answers>
<answer>
<text>memebase</text>
</answer>
<answer>
<text>funnyjunk</text>
</answer>
<answer>
<text>4chan</text>
</answer>
<answer>
<text>quickmeme</text>
</answer>
</answers>
</question>
<question>
<ask>What is the name of the comedy show in which this image first appeared?</ask>
<correctanswer>Panic Face King</correctanswer>
<picture>question4.jpeg</picture>
<answers>
<answer>
<text>Takeshi Castle</text>
</answer>
<answer>
<text>Panic Face King</text>
</answer>
<answer>
<text>Spank the laugh</text>
</answer>
<answer>
<text>The Big Moment</text>
</answer>
</answers>
</question>
<question>
<ask>What is the name of the person above?</ask>
<correctanswer>Ayumu Kato</correctanswer>
<picture>question4.jpeg</picture>
<answers>
<answer>
<text>Katashi Samosoro</text>
</answer>
<answer>
<text>Konno Miko</text>
</answer>
<answer>
<text>Ayumu Kato</text>
</answer>
<answer>
<text>Nakamura Asuka</text>
</answer>
</answers>
</question>
</quiz>