2

I'd like to input some code, for example a menu in HTML from another file so that I can edit that menu and then all the menus for all the sites would change as they'd be linked to that page. Is there a way to do so without making all the pages .php?

6 Answers 6

3

Server side includes are going to be the best way to do this, but if that's really not an option you could do it with JavaScript - load the contents of another file using AJAX when the first page loads, and insert that content into a specified element on the first page.

For example (using jQuery, because it's simpler to write out here):

$.get('page2.html', function(data) { $('#whereToPutContent').html(data); });

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to make all the pages in php. As long as the page you're going to include doesn't have php code, it can be pure html, or txt, or whatever.
The include HAS to be in a php page, that's all.

So, in your PHP page, just use include (or require) and you're set. For example:

<?php 
include ('menu.html');
?>

Comments

1

Yes, it's possible with frames or AJAX (use <script src>). However, frames are deprecated and AJAX is only reliable if the browser has JavaScript enabled.

So PHP is the (only) solution here. Here are the four possibilities:

<?php
include 'menu.html';
require 'menu.html';
include_once 'menu.html';
require_once 'menu.html';
?>

You probably want to use include_once for a menu so that it is only include one time. Or if you are sure that it is only included one time you can just use include. require stops the script if it can't find the file, so that's probably not what you want.

Comments

0

You may use server-side includes (SSI):

include ./includes/include.html

or

include ./includes/include.ssi

or

include ./includes/include.shtml


iframes:

<iframe src="http://website.com/index.html">
    <p>Your browser does not support iframes.</p>
  </iframe>

or javascript (AJAX):

STEP 1: add code to html file

<script language="JavaScript" SRC="http://yourwebsite.com/js/file.js"></script>

STEP 2: configure [apache] server by adding to .htaccess file. Add this line:

AddType application/x-javascript .js

Comments

0

The pages including the menu page would have to be PHP yes, the menu page itself doesn't have to be. For this you can use: include()

include './file.html';

Comments

0

you can dynamically display menus (or whatever) with JavaScript.

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.