PHP redirect no longer working with counter

Hey all, so recently I have been setting up a little quiz in PHP and it runs off of a counter. When a user gets a question right the counter goes up and displays the next question if not the counter returns to 0 and sets the user back to the first question to start over. I decided to add echos of html image code so when the user gets a question right it will display an image according to what the answer is. Doing this however broke my redirect, I basically had it set up so when it reaches the last question it redirects to a page and it was working until the addition of the images. Here is my total php code for not including the arrays as they are not important for my issue:

// current question
$currentQuestion = 0;

if(isset($_POST["currentQuestion"])){
     $currentQuestion = $_POST["currentQuestion"];
     if(isset($questionsAndAnwsers[$currentQuestion])){
     $currentAnswer = $questionsAndAnwsers["$currentQuestion"]["answer"];

     if($currentQuestion==17){
        header("Location: http://students.purchase.edu/martin.mcnicholas/scriptingfortheweb/index2.html"); /* Redirect browser */
        exit();
     }else if($_POST["guess"] == $currentAnswer){
         $currentQuestion++;
         $guess = $_POST['guess'];
         print ("<span class='Stylize2'>Your answer: $guess <br>"); 
         print("The answer expected: $currentAnswer<br>");  
         print("Answer Correct $answerCorrect<br><br>");
         if($currentQuestion==1){
         echo '<img src=MickeyMouse.png height="200"><br><br>';
       }
       if($currentQuestion==2){
         echo '<img src=Philo.jpg height="200"><br><br>';
       }
       if($currentQuestion==3){
         echo '<img src=warner.jpg height="200"><br><br>';
       }
       if($currentQuestion==4){
         echo '<img src=superman.jpg height="200"><br><br>';
       }
       if($currentQuestion==5){
         echo '<img src=own.jpg height="200"><br><br>';
       }
       if($currentQuestion==6){
         echo '<img src=scooby.png height="200"><br><br>';
       }
       if($currentQuestion==7){
         echo '<img src=garfield.jpg height="200"><br><br>';
       }
       if($currentQuestion==8){
         echo '<img src=pok1.jpg height="200"><br><br>';
       }
       if($currentQuestion==9){
         echo '<img src=peanuts.jpg height="200"><br><br>';
       }
       if($currentQuestion==10){
         echo '<img src=sam.jpg height="200"><br><br>';
       }
       if($currentQuestion==11){
         echo '<img src=haim.jpg height="200"><br><br>';
       }
       if($currentQuestion==12){
         echo '<img src=back.jpg height="200"><br><br>';
       }
       if($currentQuestion==13){
         echo '<img src=joe.jpg height="200"><br><br>';
       }
       if($currentQuestion==14){
         echo '<img src=r.png height="200"><br><br>';
       }
       if($currentQuestion==15){
         echo '<img src=wild.jpg height="200"><br><br>';
       }
       if($currentQuestion==16){
         echo '<img src=car54.png height="200"><br><br>';
       }
         print("Next Question Below<br></span><br><br>");
       } 
       else {
         $currentQuestion=0;
         $guess = $_POST['guess'];
         print ("<span class='Stylize'>Your answer: $guess <br>");   
         print("You have failed..<br>"); 
         echo '<img src=angry.gif height="200"></span><br><br>';
       }
       }else{
          exit("Question not found!");
       }
    }

As soon as the if statements for the images were added it no longer works and I don’t understand why.

Is there anything before your opening <?PHP tag? Any HTML, blank space, anything like that?

You have taken into account presumably that as soon as you send anything to the browser, you can’t send any header() stuff any more, if you have it configured it will show a “headers already sent” error when you attempt to redirect. I can’t see your form, or anything other than this specific bit of code of course, but sending stuff to the browser at the wrong time is the primary reason for redirects failing.

As for these repeated sections to display an appropriate image:

 if($currentQuestion==2){
         echo '<img src=Philo.jpg height="200"><br><br>';
       }

put the image name in an element in the $questionsandAnswers[] array, and you can just have this section of code once. You can replace that entire section with

echo '<img src="' . $questionsAndAnswers[$currentQuestion]["imagename"] . '" height="200"><br><br>';

So as I did mention I did I did leave out the arrays which are placed above the current question counter other than that there is no code above my PHP other than the standard html tag with css. The idea is to have the images appear depending on what the question counter was since that tells what number question it is on. After reaching the last question of 16 when the submit button is hit and the counter reaches 17 the page should redirect itself.

So there is nothing before the php or the header redirect other than the arrays but the header does redirect on counter 17 whereas all of the image echos do take place before hand. I had no issues with the redirect before however two things I added after the fact where the images and the error checking. I assumed the echo was interfering with the header but the header is placed first and so I don’t know how that then interferes.

But then there is the fact that I added the error checking which does wrap around the entire code but that shouldn’t affect it either. Just for the sake of showing it here is my code in its entirety including the html css everything:

<html>
<head>
<title>
TV Quiz
</title>
<style type="text/css">
#main{
  font-family:"Times New Roman", Times, serif; 
  font-size:2em;
  text-align:center;
  border-style: solid;
  border-color: black;
  border-radius: 15px;
  border-width: 1px;
  background: white;
}
.Stylize {
  color: red;
  text-align:center;
  display: block;
 
}
.Stylize2 {
  color: green;
  text-align:center;
  display: block;
  
}
</style>
</head>
<body bgcolor="yellow">
<?php
$questionsAndAnwsers = array(array("question" => " What early cartoon character created by the Disney Studio was Mickey Mouse based off of ?", "answer" => "Oswald The Lucky Rabbit"),

array("question" => "Who invented the first TV ? Please use full name.", "answer" => "Philo Taylor Farnsworth"),

array("question" => "When was Warner Brothers founded ? Format: Month Day, Year", "answer" => "April 4, 1923"), 

array("question" => "When was Superman's first appearance date ? Format: Month Year", "answer" => "June 1938"),

array("question" => "What does the acronym OWN, for the cable television channel, stand for ?", "answer" => "Oprah Winfrey Network"),

array("question" => "What type of dog is Scooby Doo from the cartoon series, Scooby Doo ?", "answer" => "Great Dane"),

array("question" => "What type of food does Garfield the cat love ?", "answer" => "Lasagna"), 

array("question" => "How many Pokemon were in Generation I ? Use number not word.", "answer" => "151"), 

array("question" => "What popular cartoon/show is Woodstock from ?", "answer" => "Peanuts"), 

array("question" => "What was Jim Henson's first puppet series back in 1955 ?", "answer" => "Sam And Friends"), 

array("question" => "Who created the Mighty Morphin Power Rangers ?", "answer" => "Haim Saban"), 

array("question" => "How many Back to the Future movies were there ? Use number not word.", "answer" => "3"), 

array("question" => "What football quarterback had a short-lived television show that premiered in 1969 ?", "answer" => "Joe Namath"),

array("question" => "Where there any R rated movies in 1961 ?", "answer" => "No"), 

array("question" => "In what year did the animal-documentary series Wild Kingdom premiere ?", "answer" => "1963"), 

array("question" => "What sit-com was about two bumbling police men and their squad car ?", "answer" => "Car 54, Where Are You")); 



// current question
$currentQuestion = 0;

if(isset($_POST["currentQuestion"])){
     $currentQuestion = $_POST["currentQuestion"];
     if(isset($questionsAndAnwsers[$currentQuestion])){
     $currentAnswer = $questionsAndAnwsers["$currentQuestion"]["answer"];

     if($currentQuestion==17){
        header("Location: http://students.purchase.edu/martin.mcnicholas/scriptingfortheweb/index2.html"); /* Redirect browser */
        exit();
     }else if($_POST["guess"] == $currentAnswer){
         $currentQuestion++;
         $guess = $_POST['guess'];
         print ("<span class='Stylize2'>Your answer: $guess <br>"); 
         print("The answer expected: $currentAnswer<br>");  
         print("Answer Correct $answerCorrect<br><br>");
         if($currentQuestion==1){
         echo '<img src=MickeyMouse.png height="200"><br><br>';
       }
       if($currentQuestion==2){
         echo '<img src=Philo.jpg height="200"><br><br>';
       }
       if($currentQuestion==3){
         echo '<img src=warner.jpg height="200"><br><br>';
       }
       if($currentQuestion==4){
         echo '<img src=superman.jpg height="200"><br><br>';
       }
       if($currentQuestion==5){
         echo '<img src=own.jpg height="200"><br><br>';
       }
       if($currentQuestion==6){
         echo '<img src=scooby.png height="200"><br><br>';
       }
       if($currentQuestion==7){
         echo '<img src=garfield.jpg height="200"><br><br>';
       }
       if($currentQuestion==8){
         echo '<img src=pok1.jpg height="200"><br><br>';
       }
       if($currentQuestion==9){
         echo '<img src=peanuts.jpg height="200"><br><br>';
       }
       if($currentQuestion==10){
         echo '<img src=sam.jpg height="200"><br><br>';
       }
       if($currentQuestion==11){
         echo '<img src=haim.jpg height="200"><br><br>';
       }
       if($currentQuestion==12){
         echo '<img src=back.jpg height="200"><br><br>';
       }
       if($currentQuestion==13){
         echo '<img src=joe.jpg height="200"><br><br>';
       }
       if($currentQuestion==14){
         echo '<img src=r.png height="200"><br><br>';
       }
       if($currentQuestion==15){
         echo '<img src=wild.jpg height="200"><br><br>';
       }
       if($currentQuestion==16){
         echo '<img src=car54.png height="200"><br><br>';
       }
         print("Next Question Below<br></span><br><br>");
       } 
       else {
         $currentQuestion=0;
         $guess = $_POST['guess'];
         print ("<span class='Stylize'>Your answer: $guess <br>");   
         print("You have failed..<br>"); 
         echo '<img src=angry.gif height="200"></span><br><br>';
       }
       }else{
          exit("Question not found!");
       }
    }
?>
<div id=main>
<br><br>
<form method="POST" action="">
    <label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];
    ?></label>
    
    <input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>"><br>
    <input type="text" name="guess" value="" placeholder="Answer...">
    <input type="submit" value="Next Question">
</form>
</div>
</body>
</html>

I have managed to fix it, after going through and cleaning up the image code by using a key array instead I found out that I was miscalculating the array. my redirect was pointing to a question that did not exist and once I cut it down to 15 instead of 17 it successfully redirected.

That is output to the browser, any html is, not just the things you see.
You should not be sending headers out after that.

I fixed the issue but could you please explain what you mean a bit more. Do you mean all of my html should be after the php not like it is above? I am not getting what you are saying

When you use header in php, there must be no output at all to the browser before that line of code. That includes just one space character or empty line.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
http://php.net/manual/en/function.header.php

Although in best practice, php programming logic and html output should be kept separate anyway, which among other organisational benefits, negates that problem.

In this topic @spaceshiptrooper tries to explain the basic concept of that in a simplified way.

3 Likes

Ah ok thank you so much I will restructure it and keep it in mind for any other projects I take on

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.