-4

I have an html file called main.html

main.html

<!DOCTYPE html>

<!--PHP goes here-->

</html>

And a PHP file called hello.php

hello.php

<?php

echo "<p>Hello!</p>";

?>

I want the html file to open the php file, and put it into the webpage so that i get this...

<!DOCTYPE html>

<p>Hello!</p>

</html>

What is the literal code to do this?

NOTE: I'm trying to get the actual PHP file in there. I DO NOT want this ...

<!DOCTYPE html>

<!--I already know how to do this!-->
<?php echo "<p>Hello!</p> ?>

</html>

Thanks

NOTE: main.html is an actual html file on a physical server. I've just simplified it.

user3557909
  • 25
  • 1
  • 4
  • http://www.php.net/manual/en/function.include.php – baarkerlounger May 16 '14 at 02:21
  • You can't run server-side code in a plain HTML file (except with SSI) – SLaks May 16 '14 at 02:22
  • well actually you can't embedd the php into html you have to embedd html into php . and that can be done using include or require. In HTML you can get the output using javascript(i.e. with the help of Ajax) – ashishmaurya May 16 '14 at 02:23
  • This is an html file on a physical server. I've just simplified it. – user3557909 May 16 '14 at 02:23
  • [`jQuery.load()`](https://api.jquery.com/load/) ? – Darren May 16 '14 at 02:37
  • If I understand you correctly, if you're trying to run an `.html` file and include an `.php` file, you can't and you can. Unless you've instructed Apache to treat `.html` files as PHP, you won't be able to do that. You can do the reverse being using an `.php` file and include an `.html` file. As SLaks already stated, this is possible, yet the file would need to have the `.shtml` file extension. – Funk Forty Niner May 16 '14 at 03:27

3 Answers3

0
<?php include 'hello.php'; ?>

To meet the minimum word requirement, I may as well say that "include" literally just includes a file in the page, and if it is php, it is then parsed too.

Oh, also, it's been a long time since I've done any web programming. Change the file extension from .html to .php! (I wouldn't have caught that without someone else mentioning it)

Lind
  • 71
  • 5
  • 3
    It should be noted that unless you modify your server configuration, you will NOT be able to execute PHP code in a .html file – scrowler May 16 '14 at 02:23
  • how can you include a php file into HTML it's not gonna be parse. – ashishmaurya May 16 '14 at 02:24
  • Yeah, sorry guys, been a long time, and the moment I noticed, I had to rush off and do something else (school stuff) – Lind May 16 '14 at 02:36
0

An HTML file cannot include PHP. You would need to rename your main.html file to main.php and then include/require the file. Also you'll want to open the HTML tag after you declare the doctype (since you closed it at the end) :)

<!DOCTYPE html>
<html>

<?php
include "hello.php";
?>

</html>

If you want information on the differences between include/require/include_once/require_once see here: Difference between require, include and require_once?

Community
  • 1
  • 1
klidifia
  • 1,287
  • 10
  • 18
  • If i just rename main.html to main.php will the HTML code execute properly? – user3557909 May 16 '14 at 02:39
  • Yes, HTML code will always execute properly. If you keep it as HTML it will simply output your PHP code for everyone to see. If it is named as .php the web server will execute the PHP. – klidifia May 16 '14 at 02:43
-1

You could use jQuery...

<!DOCTYPE html>

<head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
    jQuery(document).ready(function(){
        jQuery("#phpPage").html('<object data="hello.php">');
    });
</script>
</head>

<body>

<p>Hello!</p>
<div id="phpPage"><\div>

</body>
</html>
Chad
  • 154
  • 10
  • The down vote is interesting, seeing as I'm the only one who provided a solution that doesn't involve PHP as the OP asked. This is how you include a PHP page in an HTML page. – Chad May 16 '14 at 16:14