douchunjing6587 2016-01-15 10:04
浏览 33
已采纳

PHP / HTML结构方式

I have a sample index.php page like this:

<?php

Define("_DEF",1);

require_once "database.php";
require_once "session.php";
require_once 'functions.php';

if (isset($_GET['logout'])) {
    $session->logout();
} else if (isset($_GET['add'])) {
    $addFlag = 1;
}


if(!$session->is_logged_in())
    redirect_to("login.php");


?> 

<html>
<header>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">


</header>
<head>
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
    <meta charset="utf-8">
    <title>Messaging Panel</title>
    <link href="style_index.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="iconic.css" media="screen" rel="stylesheet" type="text/css" />
    <script src="prefix-free.js"></script>
</head>
<body>

    <div class="wrap">

        <nav>
            <ul class="menu">
                <li><a href="#"><span class="iconic home"></span> Home</a></li>
                <li><a href="index.php?add"><span class="iconic plus-alt"></span> New Message</a></li>
                <li><a href="#"><span class="iconic mail"></span> List Messages</a></li>
                <li><a href="index.php?logout"><span class="iconic user"></span> Logout</a></li>
            </ul>
            <div class="clearfix"></div>
        </nav>
    </div>
    <?php if (isset($addFlag) && $addFlag==1){ ?>
        <h3>Add Message HTML Goes Here!</h3>

    <?php } 
</body>
</html>

I have several HTML forms for different action. E.g. when user calls index.php?add I want to display a custom form to add new message into my database.
My current code as you can seed will be so complex if I'm goint to have several actions in my index.php and it will looks like a big if-else structure which be hard to debug. I want to know are there any structured method to have specific HTML for each situation and then include it based on PHP variables status? Currently I know I can call a function to display the form using echo HTML_TAGs , but are there any methods to have HTML form in seperate file and php functions bring it to HTML based on passed variables?
My main target is to have a structure of files for each situation (one for add record, one for list records , ...).
Note: Currently I don't know anything about jQuery. So I look for simple HTML+PHP solutions! :)

  • 写回答

4条回答 默认 最新

  • dqprf0976 2016-01-15 10:15
    关注

    You can use require_once,require,include and include_once

    Sample form

    addForm.php

    <form action="tosend.php">
      <!-- Sample elements here -->
      <input type="text" name=""/>
    </form>
    

    then in your php file where you want to include html file just use require_once or include_once

    Sample

    if(condition){
       //condition is meet include the form
       include_once('location/addForm.php');
    }else{
       include_once('includeAnotherForm.php');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块