Jump to content

Recommended Posts

Hi everybody, I created a trivia game which lets the user answer questions. So far I have created a skeleton version of it. The problem I am having is my variables are not saving in an array - the way I would like them to. I have a session array created. Also if you are using it the questions aren't answered yet. Here is my code :

 

<html>
<head>
<title>Trivia</title>
</head>
<?php
//Hides non-harmful errors
error_reporting(E_ALL ^ E_NOTICE);
//Gets the content from the question text file
$file = $_SERVER['DOCUMENT_ROOT'] . "/class/Assignment1/questions.txt";
$contents = file($file);
//Session Start
session_start();
 
$answerOne = $_POST['answerOne'];
$answerTwo = $_POST['answerTwo'];
$answerThree = $_POST['answerThree'];
$answerFour = $_POST['answerFour'];
$answerFive = $_POST['answerFive'];
$answerSix = $_POST['answerSix'];
 
//Declaring my session variables for answers/questions
$_SESSION['answers'] = array($answerOne, $answerTwo, $answerThree, $answerFour, $answerFive, $answerSix);
$_SESSION['contents'] = array($contents[0], $contents[1], $contents[2], $contents[3], $contents[4], $contents[5]);
$answerArray = $_SESSION['answers'];
$questionsArray = $_SESSION['contents'];
//Declaring my variables
$answer = "answerOne";
$text = "text";
$submit = "submit";
$questions = $questionsArray[0];
 
//If the button is clicked.
if (isset($_POST['submit']) == true ){
        $clickCount = intval($_POST['clickCount']);
        $clickCount += 1;
        $questions  = $questionsArray[1];       
 
//If the clickCount = 1
if($clickCount == 1){
            $answer = "answerTwo";
        
//If the clickCount = 2
        }if($clickCount == 2){
            $answer = "answerThree";
            $questions = $questionsArray[2];
 
//if the clickCount = 3
        }if($clickCount == 3){
$answer = "answerFour";
$questions = $questionsArray[3];
 
//If the clickCount = 4
}if($clickCount == 4){
$answer = "answerFive";
$questions = $questionsArray[4];
 
//If the clickCount = 5
}if($clickCount == 5){
$answer = "answerSix";
$questions = $questionsArray[5];
 
//If the clickCount = 6
}if($clickCount == 6){
$text = "hidden";
$submit = "hidden";
$questions = "";
            print_r($answerArray) . "<br />";
           }
}
 
 
 
?>
<body>
<form action="trivia1.php" method="post">
<input type="hidden" name="clickCount" value="<?php echo $clickCount; ?>">
<label><?php echo $questions; ?></label>
<input type="<?php echo $text; ?>" name="<?php echo $answer; ?>">
<input type="<?php echo $submit; ?>" name="submit">
</form>
</body>
 
</html>

There are no such thing as non-harmfull errors, don't suppress errors fix them!

 

As for your question, which part of the script is failing, and how?

 

 

Also, HTML allows you to use an array-style name for fields, so you can just use $_POST['answers'] and get all the answers given, even with indexes that represent the question numbers or names.

Edited by vinny42

It seems that you want to answer a number of questions  on separate pages? Yet you alsways look at all the questions at once in $_POST and use that to fill the session. So if question one is not sent along when you answer question 2, then question 1 will be erased.

Im guessing this is a screenshot from the last question?

 

Because your code keeps track of a clickcounter so you are adding one question at a time, but you are loading all questions from $_POST every time.

So, when you getto question six you are loading the first five questions from $_POST, but they don't exist on that page, so you are filling them with NULL.

 

The solution to this problem is simple; put a hidden field in the form and fill it with thw questionnumber. When the form is submitted, use that number as an index in the arrays, and increase it by one to display the next question form.

 

Only process the question that you have in $_POST, and *check* that the data you are trying to get from $_POST actually exists where!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.