Devashish Prasad
Devashish Prasad

Reputation: 1287

html is not including link and scripts when included through php

I m beginner in coding with php, I came across include and require statements and started implementing them. I included a HTML file in my PHP file. The sample code of my HTML file is (just to illustrate the scenario not he actual code) -

<html>
   <link href="some/css/file.css" />
   <script src="javascript/file.js"></script>
   <body>
        <!--body of the document-->
   </body>
</html>

code for php file -

<?php
   include("file.html");
?>

And the output is plain html without any effect of css and javascript on it although i included it in the HTML file. So, why is that happening?

if i write php in the following way-

<style>
 <?php include("some/css/file.css");?>
</style>

<script>
   <?php include("javascript/file.js"); ?>
</script>

<?php
   include("file.html");
?>

it works fine..... But if i want to include 10 css files and 10 js files... it will be an overhead to include the files individually.....

And why the first approach does not work?

Thanks for reading.......

Upvotes: 1

Views: 59

Answers (3)

sunben
sunben

Reputation: 95

you missed to related the link as stylesheet

<link rel="stylesheet" type="text/css" href="path/to/css">

Upvotes: 1

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7171

you can try this way

create a html file which will include .css and .js file and this html file you add on your php page like

file.html //have .js and and .css file

<link rel="stylesheet" type="text/css" href="some/css/file.css" />
<script src="javascript/file.js"></script>

and this file.html include on php like

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

Upvotes: 2

Ronnie Oosting
Ronnie Oosting

Reputation: 1252

To start with you could make a defines.php file and include it in your index.php. What you could add is this: define('BASE_PATH', dirname(__FILE__));

This means when you enter for example this: $this->basePath = BASE_PATH . '/lib/company/Layouts/'; you will always have the right base_path incase you switch servers.

Which means you can change your script to this:

<style>
 <?php include __BASE_PATH__ . "some/css/file.css";?>
</style>

<script>
   <?php include __BASE_PATH__ . "javascript/file.js"; ?>
</script>

<?php
   include __BASE_PATH__ . "file.html"; // or include $this->basePath . "file.html"; This depends on how you want to use this. 
?>

Somehow you still need to edit your files only once and the defines.php could be the solution for your next projects or in future current project.

I hope this helped you.

Upvotes: 1

Related Questions