在php中生成测验项目,但标题函数未按预期工作
我的index.php页面看起来像在php中生成测验项目,但标题函数未按预期工作,php,Php,我的index.php页面看起来像 <?php include("header.php"); $nameerror = $courseerror = ""; if($_SERVER['REQUEST_METHOD'] == "POST"){ if(isset($_POST['name']) && !empty($_POST['name'])){ $name = $_POST['name']; }el
<?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"); ?>
我的首页是这样的。那我怎么搞不懂
<?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">
您可以在调用header函数之前输出内容。基于php.net文档 请记住,在发送任何实际输出之前,必须调用header,无论是通过普通HTML标记、文件中的空行还是从PHP。使用include或require函数或其他文件访问函数读取代码,并且在调用头之前输出空格或空行,这是一个非常常见的错误。使用单个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['stname'],而是检查了$_POST['name']。因此,解决方案就是简单地检查正确的字段 如果您先输出内容,则无法使header函数工作。你有一些可能的解决办法 将PHP代码放在任何html脚本之前。 @knets告诉过你使用ob_start和ob_end_flush。 如果你只关心重定向,那么就使用元标记重定向。 样本:
if(isset($name) && isset($course)){
$_SESSION['name'] = $name;
$_SESSION['course'] = $course;
print "<meta http-equiv='refresh' content='0;url=questions.php' />";
}
确保在标题函数I'm not但不起作用之前不打印任何内容。它将重定向到同一index.php页面,而不是questions.php页面。在重定向部分不打印任何内容之前,您是否会告诉我您需要的header.php?你的html代码是以tag开头的吗?好的,我会编辑这个问题并发布标题部分,但是我没有看到有人尝试放置includeheader.php;检查请求方法是否为POST的if语句后的部分执行了相同的操作,但仍然不工作确保在之前没有空格和行,也请确保您的错误报告显示警告。仍然不工作没有空格everi放置了什么;但仍然不起作用我不知道还有一个问题,就是index.php所在的文件夹中的questions.php?嘿@ivoid,问题解决了,是name属性
if(isset($name) && isset($course)){
$_SESSION['name'] = $name;
$_SESSION['course'] = $course;
print "<meta http-equiv='refresh' content='0;url=questions.php' />";
}