在PHP中构建测验项目,但标题功能未按预期工作

时间:2017-12-17 16:53:17

标签: php

我的index.php页面看起来像

<?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"); ?>

但问题是当我使用头部函数将其重定向到我的questions.php页面时,它不会重定向到questions.php它再次加载相同的index.php页面而这里没有其他内容是上面的代码

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

    } 

我的标题页看起来像这样。所以这里有什么不对,我无法得到它?

<?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">

2 个答案:

答案 0 :(得分:-1)

在调用header()函数之前输出内容。基于php.net文档

header() documentation

  

请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。使用include或require,函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用header()之前输出空格或空行。使用单个PHP / HTML文件时存在同样的问题。

所以为了避免这种情况,你可以打开输出缓冲

所以你的index.php看起来像这样

<?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
?>

修改 因为这个答案被接受了我想添加这个信息,问题不是在输出内容之后调用了header()函数但是有一个逻辑问题(或拼写错误)而不是检查$ _POST [&#39 ; stname&#39;]他正在检查$ _POST [&#39; name&#39;]。因此,解决方案是简单地检查正确的字段。

答案 1 :(得分:-1)

如果要在其之前输出内容,则无法使标头功能起作用。你有一些可能的解决方案。

  1. 将PHP代码放在任何html脚本之前。
  2. @knets告诉你使用ob_start()和ob_end_flush()。
  3. 如果您不关心其他任何事情,只需重定向,请使用元标记重定向。
  4. 样品:

    if(isset($name) && isset($course)){
       $_SESSION['name'] = $name;
       $_SESSION['course'] = $course;
    
       print "<meta http-equiv='refresh' content='0;url=questions.php' />";
    }