0

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! :)

3
  • 1
    Have you taken a look into any templating engines? Commented Jan 15, 2016 at 10:07
  • @Emn1ty You mean something like Smarty? Are there any other ways to conditionally include .html files somewhere in my HTML file like php includes? Commented Jan 15, 2016 at 10:08
  • broculos.net/2008/03/… this is a fairly old post, but it does give you some ideas about how to roll your own, simple templating engine. Otherwise as other answers have stated using require or include will work. Commented Jan 15, 2016 at 10:14

4 Answers 4

1

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');
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can have HTML elements in different files. Than you can require('file.ext') to display that page.

<?php
require(__DIR__.'/header.php');

if (isset($_REQUEST['page']) && file_exists(__DIR__."/pages/{$_REQUEST['page']}")) {
    require(__DIR__."/pages/{$_REQUEST['page']}.php");
} else {
    require(__DIR__."/pages/home.php");
}

require(__DIR__.'footer.php');
?>

Comments

1

Personally, since you already have separate files for session.php, etc. I'd probably write a view.php as well, with the following setup:

function view($name)
{
    $parts = explode('.', $name);

    $template = '/views/' . implode('/', $parts) . '.php';

    require_once __DIR__ . $template;
}

Then you can use it like so, storing all your view files in the ./views directory:

require_once './view.php';

view('home.message'); // {path}.{to}.{file} => /{path}/{to}/{file}.php

You can also write it as a class if you so wish.

PS - this code above was not tested, but the principle is sound and is aimed at creating a view file like this:

<!DOCTYPE html>
<html>
<head>
    <?php view('layout.head'); ?>
</head>
<body>
    <div class="wrap">
        <?php view('layout.menu'); ?>
    </div>

    <!-- load conditionally -->
    <?php if (isset($addFlag) && $addFlag == 1) view('add.message'); ?>

    <?php view('layout.footer') ?>
</body>
</html>

Comments

0

I'd should simple enough to use the INCLUDE statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.