1

我正在为一个班级制作一个网页。这是一个为即将到来的新生设计的网站,用于接收他们感兴趣的专业的信息。

对于作业,我必须将我的原始 HTML 代码分成 header.php、content.php 和 footer.php。我还必须通过 $ SERVER[PHP SELF] 的操作为我的表单使用 POST 方法。我还必须在表单中使用隐藏的输入字段。

我已经完成了所有这些任务,但我认为我的 heredoc 打印有问题 <<

我知道我的表单还有一些工作要做,但我真的很想看到页面将正确加载我的页眉、内容和页脚。由于我什么都看不到,我不想继续写出任何其他功能。请您看看我的 PHP 代码并帮我找到这个错误好吗?我确定我只是忽略了一些东西,但我会很感激你的帮助!谢谢!

头文件.php:

<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>UNA Preview Day 2013</title>
<link rel="stylesheet" href="preview.css">
<script src="preview.js"></script>
</head>
<body>

内容.php:

<!-- Tell script impaired users we *must* have Javascript -->
<noscript>
    <p class="alert">Sorry, this page requires Javascript!</p>
</noscript>

<img src= "banner.gif" alt = "UNA Logo" />
<br />
<h1>UNA Preview Day 2013</h1>

页脚.php

    <div class = "image">
    <img src = "unapic.png" alt = "UNA Logo" />
</div>
<p id = "footer">Copyright &copy; 2013 | University of North Alabama</p>
</body>
</html>

索引.php:

<?PHP
require("header.php");

if ($_POST[_submit_check])  { 
processForm();
}
else  {
displayForm();
}

function displayForm()  {
require("content.php");
}

require("footer.php");

if (array_key_exists('_submit_check', $_POST))  {
processForm();
}
else  {
displayForm();
} 

// ------------------------------------------------
function processForm()  {    //NEEDS WORK
print "Hello, $_POST[user]!";
}

function display_form()  {
//require("content.php");  

print <<<HTMLBLOCK
 <html>
    <head> 
    <title>UNA Preview Day 2013</title>
    </head>
    <body>
    <form name = "contact" method = "POST" action="$_SERVER[PHP_SELF]">                
    <div class = "appearance">
    *All fields required.*<br /><br />
    Name: *  <input title="Name required!" type = "text" id = "name" name = "name"        required autofocus onchange = "validName();" size = 30>
    <br />
    Email: * <input title="Email address of form username@domain required!" type =   "email" placeholder = "me@example.com" id = "email" name = "email" 
    required onchange = "validAddress();" size = 30>
    <br /><br /><br />
    Areas of Study <br />
    Check the program(s) in which you are interested in. (*Check AT LEAST one.*) <br />
    <input type = "checkbox" id = "cs" name = "option[]">Computer Science<br />
    <input type = "checkbox" id = "cis" name = "option[]">Computer Information Systems<br     />
    <input type = "checkbox" id = "ddep" name = "option[]">Dual Degree Engineering    Program<br /><br /><br />
    <div class = "center">
        <input type="button" value="Send" onClick = "return validOption()">
        <input type="reset" value="Clear">
    </div>
    <input type = "hidden" name="_submit_check" value="1">  
    </div>
    <br />
    <br />
    </form>
    </body>
</html> 
HTMLBLOCK;  
}
?>
4

2 回答 2

1

我在下面给你做了一些非常难看的东西。但是,是的,您需要在调用它们之前声明所有函数,因此将它们放在头文件中,甚至更好地放在单独的文件中和require_once('functions.php');头文件的最顶部。

您将函数拼写为 displayForm(),然后将 display_form() 放低。确保遵守命名约定,否则它将不起作用。

还要确保只保留 1 个标签,<html><body><title>即使您有多个文件,如果有意义的话,整个文档只需要一个。

检查是否提交时。我改为 if(isset($_POST['_submit_check']){} 但对你有用。

Netbean 是我最喜欢的文本编辑器,它可以帮助您将来进行调试。

<!------- Start of header.php --------->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UNA Preview Day 2013</title>
<link rel="stylesheet" href="preview.css">
<script src="preview.js"></script>
</head>
<body>
<!-- Tell script impaired users we *must* have Javascript -->
<noscript>
    <p class="alert">Sorry, this page requires Javascript!</p>
</noscript>

<img src= "banner.gif" alt = "UNA Logo" />
<br />
<h1>UNA Preview Day 2013</h1>

<?php

function displayForm()  {
//require("content.php");  
$htmlblock = '
    <form name = "contact" method = "POST" action="'.$_SERVER["PHP_SELF"].'">                
    <div class = "appearance">
    *All fields required.*<br /><br />
    Name: *  <input title="Name required!" type = "text" id = "name" name = "name"        required autofocus onchange = "validName();" size = 30>
    <br />
    Email: * <input title="Email address of form username@domain required!" type =   "email" placeholder = "me@example.com" id = "email" name = "email" 
    required onchange = "validAddress();" size = 30>
    <br /><br /><br />
    Areas of Study <br />
    Check the program(s) in which you are interested in. (*Check AT LEAST one.*) <br />
    <input type = "checkbox" id = "cs" name = "option[]">Computer Science<br />
    <input type = "checkbox" id = "cis" name = "option[]">Computer Information Systems<br     />
    <input type = "checkbox" id = "ddep" name = "option[]">Dual Degree Engineering    Program<br /><br /><br />
    <div class = "center">
        <input type="submit" value="Send" onClick = "return validOption()">
        <input type="reset" value="Clear">
    </div>
    <input type = "hidden" name="_submit_check" value="1"/>
    </div>
    <br />
    <br />
    </form>';
    return $htmlblock;
}
function processForm()  {    //NEEDS WORK
$message= "Hello, ";
if(isset($_POST['name'])){
    $message.=$_POST['name'];
}
return $message;
}
?>
//// END OF HEADER  ///////////
//// START OF CONTENT //////////
<?php
if (isset($_POST['_submit_check']))  {



echo processForm();
}
else  {
echo displayForm();
}
?>
<!---- END OF CONTENT ------>
<!---- START OF FOOTER ------->

</body>
</html>

<!----- END OF FOOTER  ------->
于 2013-10-27T16:23:30.273 回答
1

有一个:

require("footer.php");

在创建无限循环的footer.php 中

于 2013-10-27T16:01:00.843 回答