Jump to content

Such A Simple Question, I Shouldn't Need Help...


CarpeDiem

Recommended Posts

I've studied most of the w3schools tutorials and have doubled back for a second swing through some of my beginning studies with HTML, XHTML, CSS, JavaScript, SQL and PHP, getting my website initially set up (I've studied many more tutorials than this, but these are the ones I've doubled back on). The HTML and CSS work just fine on my website, and the JavaScript and SQL will be needed after I get PHP working. Here's my problem: I'm now ready to implement the PHP (which I read through just fine in the tutorials and I felt I understood too), but when I got to my real website, my very first initial test using PHP doesn't seem to implement as expected.Here's what I did. I copied the following code directly from the w3schools' PHP Include File chapter:

<html><body><?php include("hello.php"); ?><h1>Welcome to my home page!</h1><p>Some text.</p></body></html>

...named it index.html so it would run when I typed in the name of my website, but I changed the include file name within it to hello.php (instead of the header.php name originally shown in the tutorial example, because of the following).I then copied from PHP.net the following PHP script:

<html> <head>  <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?>  </body></html>

...and I named this test script to hello.php as PHP.net suggested.I would anticipate, when I typed in the name of my website into my browser, that I would see the combined result of these two files like

Hello WorldWelcome to my home page!Some text
but I only get the two lines from the html page and the include file does not show when I enter the name of my website into my browser. If I add "hello.php" to the name of my URL, then I do get "Hello World" alone on the screen. However, I never get the 3 lines together.Gee, I think I've done everything by the book(s) (everything's direct from both w3schools and PHP.net), but I don't get a working 3-line outcome as I exampled above! Everything seems to be conceptually correct (and I've tried a variety of variations too, such as stripping out everything but the PHP code in the PHP file, and other things too, etc.). Each file does work individually as expected -- but the two files do not function together through the include PHP in the HTML file! I don't know what's wrong with this beginner's problem and I've spent hours reading and reading...but I still can't get it to work!
Link to comment
Share on other sites

Also, the "included" file does not need the <html> and <body> tags in it.Included files are treated as an Html 'snippet' and those tags are already in place in the calling file.Really, the included file only needs the following in it.

<?php echo '<p>Hello World</p>'; ?>

Link to comment
Share on other sites

Well, that information hit the target!The title information in the head section of the include file that I got from PHP.net did show up on the title bar of my browser, but when I took everything out of the the include file as jlhaslip suggested except the PHP code, the files combined just fine (but, of course, the title then went away).The view of the source code that resulted in my browser for these original combined files is interesting, as it has two opening html and two opening body tags, with the include file's head tag after the 2nd html tag:

<html><body><html> <head>  <title>PHP Test</title> </head><body><p>Hello World</p></body></html><h1>Welcome to my home page!</h1><p>Some text.</p></body></html>

What a jumble of code, but it worked, and who knows, maybe being able to have head information in another included php file might be useful. On the majority, I'll follow jlhaslip's advice and only include the snippet I need, so that I jumble less.The whole key was not using an index.html file as JustSomeGuy suggested and using only an index.php file instead (and again, I didn't see this information in the tutorials -- but thank goodness it was here in the forum)! I didn't know that a site could run with no index.html file (duh!).A little more experimenting showed that if I have both an index.html and an index.php at the same time, my initial visit to the site implements the html file first. It seems if there are both index-file types (index.html and index.php), the html is of higher priority than the php -- but if there is no index.html, then the index.php implements itself in place of an index.html. How fun! Gee, what you don't pick up in theory class!

Link to comment
Share on other sites

The order of the file search and action can be changed using an .htaccess file (subject to your host allowing it).

Change Default Page (order is followed!)DirectoryIndex myhome.htm index.htm index.php

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...