简体   繁体   English

PHP:一个php文件-多页

[英]PHP: One php file - many pages

I'm supposed to make 1 PHP file called "access.php" that contains 11 pages. 我应该制作1个名为“ access.php”的PHP文件,其中包含11个页面。 I have to do this with 1 PHP file. 我必须使用1个PHP文件来完成此操作。 I'm supposed to accomplish this using functions to display each page. 我应该使用显示每个页面的功能来完成此操作。 The "welcome" page allows a user to select "Administrator" or "User" via radio button. “欢迎”页面允许用户通过单选按钮选择“管理员”或“用户”。 After hitting submit, the corresponding page should appear. 点击提交后,将出现相应的页面。 Right now the admin/user page functions simply echo "Administrator Page" or "User Page" for testing purposes. 现在,出于测试目的,管理员/用户页面功能仅回显“管理员页面”或“用户页面”。

Ideally, a new page should appear that displays one of those centered at the top of the screen. 理想情况下,应该出现一个新页面,其中一个显示在屏幕顶部居中。 But what's happening right now is the text "Administrator Page/User Page" just appears at the bottom of the welcome screen. 但是,现在正在发生的是“管理员页面/用户页面”文本刚刚出现在欢迎屏幕的底部。

Here is my code: 这是我的代码:

<?php
#---Functions---

#**PHP Header**
#function phpHeader(){
#print <<< EOF
#<!DOCTYPE html> 
#<head> 
#</head>
# <body>
#EOF
#}

#**PHP Footer**
#function phpFooter() {
#echo "</body> </html>";
#}

#**Admin Page**
function adminPage(){
#phpHeader();

echo "<center><h1>Administrator Page</h1></center>";

#phpFooter();
}

#**User Page**
function userPage(){
#phpHeader();

echo "<center><h1>User Page</h1></center>";

#phpFooter();
}

#**Welcome Page**
function welcomePage(){
#phpHeader();

echo "<center><h1>Welcome</h1>";
echo "<br><br><br>";
echo "<h3> Select Login Type</h3>";
echo "<br><br><br>";

echo '<form name="access" action="" method="post">';
echo '<input type="radio" name="AccessLevel" value="admin" />Administrator <br />';
echo '<input type="radio" name="AccessLevel" value="user" /> User <br /><br />';
echo '<input type="submit" name="submit" value="Choose Level" />';
echo '</form></center>';

$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
    if (! isset($AccessLevel)) {
    echo '<h2>Select an option </h2>';
    }
    elseif ($AccessLevel == "admin") {
    adminPage();
    }
    else {
    userPage();
    }

}
#phpFooter();
}

welcomePage();

?>

Move this entire block at the top of your page, right under your opening <?php tag. 将这整个块移到页面顶部的<?php标签下面。

Sidenote: You can use return; 旁注:您可以使用return; or exit; exit; or die(); die(); , the choice is yours; , 这是你的选择; I used return; 我用return;

$AccessLevel = $_POST['AccessLevel'];
if (isset($_POST['submit'])) {
    if (! isset($AccessLevel)) {
    echo '<h2>Select an option </h2>';
    }
    elseif ($AccessLevel == "admin") {
    echo adminPage();
return;
    }
    else {
    userPage();
return;
    }

}

Which will echo only "Administrator Page" or "User Page" at the top of the page and nothing else. 它将仅回显页面顶部的“管理员页面”或“用户页面”,而没有其他显示。

<?php

function adminPage(){

    return "<center><h1>Administrator Page</h1></center>";

}

function userPage(){

    return "<center><h1>User Page</h1></center>";

}

function welcomePage(){

    return '<center><h1>Welcome</h1>
<br><br><br>
<h3> Select Login Type</h3>
<br><br><br>
<form name="access" action="" method="post">
<input type="radio" name="AccessLevel" value="admin" />Administrator <br />
<input type="radio" name="AccessLevel" value="user" /> User <br /><br />
<input type="submit" name="submit" value="Choose Level" />
</form></center>';

}


$AccessLevel = $_POST['AccessLevel'];

if(isset($_POST['submit'])){
    if(!isset($AccessLevel)){
        echo welcomePage();
    }elseif($AccessLevel=="admin"){
        echo adminPage();
        echo welcomePage(); // if you want to repeat that content at the bottom again
    }elseif($AccessLevel=="user"){
        echo userPage();
        echo welcomePage(); // if you want to repeat that content at the bottom again
    }
}else{//no form submitted

 echo welcomePage();

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM