-3

my index.php page looks like

<?php include("header.php"); 
    $nameerror = $courseerror =  "";
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        if(isset($_POST['name']) && !empty($_POST['name'])){
            $name = $_POST['name'];
        }else{
            $nameerror = "Please Enter Your Name And Continue";
        }
        if(isset($_POST['course']) && !empty($_POST['course'])){
            $course = $_POST['course'];
        }else{
            $courseerror = "Please Select Your Course And Continue";
        }

        if(isset($name) && isset($course)){
            $_SESSION['name'] = $name;
            $_SESSION['course'] = $course;
            header("Location: questions.php");
        }

    }


?>


                <table>
                <caption><?php  echo $nameerror."<br>".$courseerror; ?></caption>
                <form action="index.php" method="post">

                    <tr><td><label for="name"> Name </label> </td><td><input type="text" id="name" name="stname"></td></tr>
                    <tr><td valign="top"><label for="course"> Course </label> </td><td>
                        <select name="course"> 
                            <option value="acit"> Acit </option>
                            <option value="graphics"> Graphics </option>
                            <option value="networking"> Networking </option>
                            <option value="programming"> Programming </option>
                            <option value="adit"> Adit </option>
                        </select>
                    </td></tr>
                    <tr> <td colspan="2" ><input type="submit" value="Continue" </td> </tr>

                </form>
                </table>

<?php include("footer.php"); ?>

but the problem is when i use header function to redirect it to my questions.php page it wont redirect to questions.php it loads the same index.php page again and nothing else here is the code from above

if(isset($name) && isset($course)){
            $_SESSION['name'] = $name;
            $_SESSION['course'] = $course;
            header("Location: questions.php");
        }

    } 

and my header page look like this. so whats wrong here i cant get it ?

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
    <title>Pearl Institute Of Information Technology Online Examination</title>
    <link href="css/main.css" rel="stylesheet">
</head>
<body>
    <div id="main">
        <header>
            <img src="images/logo.png">
            <h1> Pearl Institute Online Examination System </h1>
        </header>
        <div id="content">
  • 1
    make sure you don't print anything before the header() function – knetsi Dec 17 '17 at 16:58
  • i am not but its not working it redirect to same index.php page and not to questions.php page . and there is no errors either – Rida Batool Dec 17 '17 at 17:03
  • so you tell me than **header.php** that you require before the redirect part does not print anything? and that your html code starts with tag?
    – knetsi Dec 17 '17 at 17:04
  • ok i will edit the question and post the header part as well , but i dont see any out there – Rida Batool Dec 17 '17 at 17:08
  • try placing the `include("header.php"); ` part after the **if** statement that checks if the request method is POST – knetsi Dec 17 '17 at 17:09
  • the problem is that you include the **header.php** that prints some output before the header() function – knetsi Dec 17 '17 at 17:10
  • header.php is required for index page layout without it its not going to include important parts of my page . – Rida Batool Dec 17 '17 at 17:13
  • ok it seems that if you place the `include("header.php)` after the code then you will have another problem with the session. There is a workaround you can place the function `ob_start()` in your index.php before the include and at the end you must call `ob_end_flush()` – knetsi Dec 17 '17 at 17:13
  • can explain the problem ? what should i remove ? – Rida Batool Dec 17 '17 at 17:13
  • i am not telling you to not include the header, but if you want the `header()` function to work , you must not output any content before the function call – knetsi Dec 17 '17 at 17:14
  • i did as u told me to . but still its not working – Rida Batool Dec 17 '17 at 17:19
  • ok i recreated your error...................... in the input that `` you set the name of the input as `stname` but in php code you are checking for `$_POST['name']` instead of `$_POST['stname']` – knetsi Dec 17 '17 at 17:40
  • omg it was the problem lol .. thank u so much @knets oh man – Rida Batool Dec 17 '17 at 17:44
  • problem is solved !! – Rida Batool Dec 17 '17 at 17:45

2 Answers2

-1

You output contents before calling the header() function. Based on php.net documentation

header() documentation

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. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

so to avoid this you can turn on the output buffering

so your index.php would look something like this

<?php 
    ob_start(); // turn on output buffering
    include("header.php"); 
    $nameerror = $courseerror =  "";
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        if(isset($_POST['stname']) && !empty($_POST['stname'])){//check stname instead of name because you have set that name in your input
            $name = $_POST['stname'];
        }else{
            $nameerror = "Please Enter Your Name And Continue";
        }
        if(isset($_POST['course']) && !empty($_POST['course'])){
            $course = $_POST['course'];
        }else{
            $courseerror = "Please Select Your Course And Continue";
        }

        if(isset($name) && isset($course)){
            $_SESSION['name'] = $name;
            $_SESSION['course'] = $course;
            header("Location: questions.php");
        }

    }


?>


<table>
<caption><?php  echo $nameerror."<br>".$courseerror; ?></caption>
<form action="index.php" method="post">

    <tr><td><label for="name"> Name </label> </td><td><input type="text" id="name" name="stname"></td></tr>
    <tr><td valign="top"><label for="course"> Course </label> </td><td>
        <select name="course"> 
            <option value="acit"> Acit </option>
            <option value="graphics"> Graphics </option>
            <option value="networking"> Networking </option>
            <option value="programming"> Programming </option>
            <option value="adit"> Adit </option>
        </select>
    </td></tr>
    <tr> <td colspan="2" ><input type="submit" value="Continue" </td> </tr>

</form>
</table>

<?php 
include("footer.php"); 
ob_end_flush(); // send the output and turn off output buffering
?>

EDIT Because this answer was accepted i would like to add this information, the problem was not that header() function was called after the output of the content but there was a logical problem (or misspell) instead of checking the $_POST['stname'] he was checking $_POST['name']. So the solution was to simply check for the right field.

knetsi
  • 1,291
  • 1
  • 15
  • 16
-1

There is no way to make the header function to work if you are outputting content prior to it. You have some possible solutions.

  1. Place the PHP code before any html script.
  2. @knets told you to use ob_start() and ob_end_flush().
  3. If you don't care about anything else but to redirect, use a meta tag to redirect.

Sample:

if(isset($name) && isset($course)){
   $_SESSION['name'] = $name;
   $_SESSION['course'] = $course;

   print "<meta http-equiv='refresh' content='0;url=questions.php' />";
}