Results 1 to 5 of 5

Thread: php load external html into variable question - beginner

  1. #1
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question php load external html into variable question - beginner

    Hi all.

    I would like to load a php variable with HTML markup that is stored in an external text file and then pass this markup to the innerHtml of a DIV element.

    I have this code:

    <?php

    $textfile="textfile.txt";
    $handle = fopen($textfile, "r");
    $contents = fread($handle,filesize($textfile));

    fclose($handle);


    ?>

    div = document.getElementById('contentdiv');
    div.innerHTML = '<?php print $contents; ?>';

    This is not working.
    I have tried with a plain text also, so I guess it has something to do
    with the way the file is read into the variable.
    I have tried hardcoding $content="Text" and it works as expected, so I know the markup is correct.

    Can anyone help me out with this?

    Thanks a lot for your time.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Well, you could try the following:

    Code:
    <?php
     $content = file_get_contents('textfile.txt');
    ?>
    Then using the javascript you can do this:

    Code:
    div = document.getElementById('contentdiv');
    div.innerHTML = '<?php echo $content; ?>';
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thetestingsite - Thanks for the reply - but this doesn't seem to work either.
    I am using just plain text in the external file to test it.

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Are you using the same code (the two posted above) on the same page? Also, do you have a link to the problem page?
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thanks for the reply.
    I had a syntax error in my code

    The plain text in the file is working but I could still not
    get html to display correctly.
    However, I have discovered various reasons for this including:

    quotes in the HTML string need to be escaped with \;
    carriage returns and other unwanted character codes need to be removed;
    the HTML needs to be on one line;

    If I stick to these rules it seems to be ok.

    Thanks very much for your help thetestingsite

Bookmarks

Posting Permissions

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