Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    noob question: php include breaks links in html...

    Hi,

    This is my first post, and it deals with my first use of php include.

    I am attempting to transition away from Dreamweaver and it's templating system to php. On my index page, I have included an external php file that contains only html code. It is my navigation menu. That code works fine when I use it directly in the index.html file. However, when I replace that html code on the index.html file with

    <?php include("menu.php");?>

    and insert that navigation code in the php file all links following the navigation menu are ignored. there is no recognition by web browsers that they exist. The page layout looks fine, the navigation menu works great (and it's links work), but all following links do not work.

    I am new to using php include, and have done a good 6 hours of reading today to try to fix this, but man... it's just not coming to me.

    Do I need to prefix and append <? and ?> to the external file? When I do it doesn't fix anything.

    Thanks. I'll attach whatever you need to assist.
    -josh

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    it has to be index.php

    not .html

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks,

    Yeah, that's what I read too. But I really don't want to go through our 120 pages and rename them all *.php in order to make our php templating work.

    I read somewhere (it's all a blur now) that by setting up .htaccess with the following code (which works on my hosting server) I can keep .html as the extension:


    AddType application/x-httpd-php .htm .html


    This is what I have done, and php is pulling my navigation menu from the external php file and serving it up through the index.html. All the rest of the html seems to come through fine as well, except <a href""> stuff.

    Thanks

  • #4
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by bigj6360 View Post
    Thanks,

    Yeah, that's what I read too. But I really don't want to go through our 120 pages and rename them all *.php in order to make our php templating work.

    I read somewhere (it's all a blur now) that by setting up .htaccess with the following code (which works on my hosting server) I can keep .html as the extension:


    AddType application/x-httpd-php .htm .html


    This is what I have done, and php is pulling my navigation menu from the external php file and serving it up through the index.html. All the rest of the html seems to come through fine as well, except <a href""> stuff.

    Thanks
    in php

    PHP Code:
    echo "<a href=index.html>Index<a>"
    will work

    without quotation marks around the address

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I believe that creates invalid HTML code though.
    Use single quotations and double for the properties with a simple echo '<a href="index.html">Index</a>';

    As for the addtype, yes that will work to parse .html as .php. Problem is that they are not really .php pages, which means if you change servers or webserver software it may not work. I would rename these pages instead. This is trivial though, as the command line will let you wildcard a match so you can rename with ren *.html *.php.

  • #6
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Fou-Lu View Post
    I believe that creates invalid HTML code though.
    Use single quotations and double for the properties with a simple echo '<a href="index.html">Index</a>';
    I have done it and it works. Admittedly it does come up as
    Code:
    <a href=index.php>Home</a>
    On page source, but the link still works. But I guess your way is still abit safer

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am only looking to template my header, nav menu, and footer at this time. Is it best to convert completely to PHP (and correct the resulting syntax errors that crop up) and insert html rather than just inserting the PHP I need into my html files?

    I have no intention of changing web hosts or server types. I am happy on my linux server.

    I'm sorry about my ignorance. Hopefully that will change sooner rather than later.

    thanks again.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by melloorr View Post
    I have done it and it works. Admittedly it does come up as
    Code:
    <a href=index.php>Home</a>
    On page source, but the link still works. But I guess your way is still abit safer
    I didn't say it wouldn't work, but I'm quite sure its invalid. There are situations that it wouldn't work, such as an implicit +/%20 added that's left to the HTML to encode. That will be especially bad in the case of additional properties. Definitely use ' or " to wrap any value in HTML.

    Quote Originally Posted by bigj6360 View Post
    I am only looking to template my header, nav menu, and footer at this time. Is it best to convert completely to PHP (and correct the resulting syntax errors that crop up) and insert html rather than just inserting the PHP I need into my html files?

    I have no intention of changing web hosts or server types. I am happy on my linux server.

    I'm sorry about my ignorance. Hopefully that will change sooner rather than later.

    thanks again.
    Its up to you really. .PHP pages can be used for pure HTML as well, at the cost of a preprocess. The server treats it as .php so it assumes that you have php code in it. So it will check the entire file first and parse it as php. Is it substantially slower? Not really. The pro is you can modify it to add php whenever you want without needing to worry how this file is dependent on others. I haven't written a .html file in years; only .php and .xml in regards to the web world (with PHP of course).

  • #9
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well,

    I have only dealt with php in relation to mySQL. I do not have a lot of experience with it. How difficult of a job am I looking at if I change from .html to .php. Am I going to need to do a considerable amount of work to accomplish this? I am not against doing it, it's just work.

    I would rather like to try and track down what is causing all links to die after the php call. That would solve all my current problems and I can work on going to PHP later as I become more fluent in it.

    Do I need to add php language in the menu.php file I am including, or is that taken care of by the php call istelf?

    thanks again

  • #10
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Can you post your code?

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by bigj6360 View Post
    I would rather like to try and track down what is causing all links to die after the php call. That would solve all my current problems and I can work on going to PHP later as I become more fluent in it.
    Did I miss something; I didn't realize you have an error here? Dying is not normal with an include unless its declaring a function which will fail on the second inclusion without a check first (which is why include_once is used more often). Not even a bad file path will cause an include to die, as an include is not required to continue processing.

    Do you mean that all the links are in .html format and you are now looking at renaming the page as .php? If so, then yep, that will be the cause for a 404 error. For this reason, you may want to use the addtype to convert the .html if you have a few hundred pages (sorry bad assumption on my part - all my links are dynamic so I don't need to concern myself with the extensions). Alternatively, .htaccess can rewrite the .html into .php if it can't find the .html.

    The included file itself does not need to be PHP, it can be html, or perl or whatever else you want it to be. If its PHP, and using a filesystem wrapper (which this is above), then it will treat the PHP as parsable. If its not PHP, or not filesystem wrapped, then it will treat the results of the page as the inclusion instead of the code within the page.

    So the best is to clarify what the problem is with the dying links first.

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by melloorr View Post
    I have done it and it works. Admittedly it does come up as
    Code:
    <a href=index.php>Home</a>
    On page source, but the link still works. But I guess your way is still abit safer
    It's invalid markup and you're basically recommending a bodge because you're inexperienced and don't know what else to recommend.

    Please don't be offended but you are relatively new to PHP so if you don't know how to offer solid advice that is clean code-wise, please don't. Again I don't wish to sound rude but its vital that if you're going to contribute, you know what is good, bad, correct and wrong otherwise you may lead be leading the blind.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by tangoforce View Post
    It's invalid markup and you're basically recommending a bodge because you're inexperienced and don't know what else to recommend.

    Please don't be offended but you are relatively new to PHP so if you don't know how to offer solid advice that is clean code-wise, please don't. Again I don't wish to sound rude but its vital that if you're going to contribute, you know what is good, bad, correct and wrong otherwise you may lead be leading the blind.
    I am not offended. I honestly thought that was a proper way to do it but if it is not then I learned that what I have been doing is incorrect, and I was called on it. So I am sorry for recommending a botch.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    For future ref, if you need double quotes within a double quoted string, escape them like this:

    PHP Code:
    $String "Joe said \"This is a test string\""
    The \ before the " tells php to ignore the next character.

    There is more about this in one of the links in my signature but I honestly can't remember which one so take a look at them.
    Edit:
    It's the quotes/parse error link
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #15
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by tangoforce View Post
    For future ref, if you need double quotes within a double quoted string, escape them like this:

    PHP Code:
    $String "Joe said \"This is a test string\""
    The \ before the " tells php to ignore the next character.

    There is more about this in one of the links in my signature but I honestly can't remember which one so take a look at them.
    Edit:
    It's the quotes/parse error link
    Thanks, and sorry again. I wouldn't intentionally give bad information/help.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •