Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about including html page in PHP

If I include an HTML page on a PHP page (say, index.php) with the following:

<?php
include ("../../forms/login/form.html");
?>

Then will form.php show up correctly in index.php? When I say correctly, I mean with all of its images and CSS.

The reason I am asking is because that's not the case with me. I tried it out, and it will show the form.html but without any styling...

Any help would be appreciated, thanks!

UPDATE:


Currently, I have the following on forms.html:

<link href="view.css" media="all" rel="stylesheet" type="text/css" />
<script src="view.js" type="text/javascript"></script>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />

Now, forms.html displays correctly by itself.

On index.php, I have:

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>UofS :: Residence Life Management System</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<?php
include ("html/header.html");
?>

<?php
include ("php/navigation.php");
?>
<br></br>
<?php
include ("forms/login/form.html");
?>
<br></br>
<?php
include ("html/footer.html");
?>

</body>

</html>

The only reference to forms.html is through the php include(). There is no stylesheet pointing to it. This is the page that does not load forms.html correctly. Is this not right?

Thanks!

like image 957
littleK Avatar asked Feb 09 '10 15:02

littleK


People also ask

HOW include HTML in HTML file in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Can we include HTML file in PHP file?

Anything that can go in a standard HTML file can go in a PHP include. Save any page that uses a PHP include as a PHP file with the appropriate extension (e.g., index. php).

How does HTML interact with PHP?

PHP processor scans the page, line by line. It build a processed HTML page. If it finds HTML, it passes that on as part of the processed HTML page it is building. If it finds PHP scripts, the PHP processor may or may not output HTML.

Why PHP is not working in HTML?

The answer is in fact so simple you would want to bang your head: Simply change the file extension from ". html" to ". php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.


1 Answers

The path to your style sheet is probably relative. It will need to be relative to the url in the browser (index.php). Or make it an absolute path.

Edit since your update: view.css and view.js will not be loaded because their paths are relative. Make those paths absolute or relative from the index.php page. Making them absolute will make them work whenever form.html is included from anywhere. Making the paths relative from index.php will make them work when included from there, but not when included from other directories.

Better yet, if you know you need to load those files, put the links on the index.php page and not the form.html page.

Also note that you can use paths that start with the root of your site, you do not have to include the host and domain name:

<link href="/css/style.css" rel="stylesheet" type="text/css" />

The slash before "css/style.css" will make that an absolute path that will work on your live and dev servers.

Edit...

Assuming that your index.php file is at the root level, try this in forms.html:

<link href="/forms/login/view.css" media="all" rel="stylesheet" type="text/css" />
<script src="/forms/login/view.js" type="text/javascript"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css" />
like image 168
Scott Saunders Avatar answered Oct 11 '22 07:10

Scott Saunders