SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display HTML output in Javascript application

    Hello,

    I have the following problem. I have a PHP script "display.output.php" that produces a certain HTML output. I use this script in several other scripts. When I want to display this output I call the script like this:

    include("display.output.php");

    But now I'm building another PHP script "javascript.php" script with a "Content-Type: application/x-javascript" header. So basically I use this script in a javascript code. What I need is to display the output of the script "display.output.php" in the script "javascript.php". I don't know how to do this. I've tried the following but it doesn't work.

    PHP Code:
    ?>
    <!-- 
    document.writeln("<?php echo addslashes(@include("display.output.php")); ?>");
    // -->
    <?php
    Like I said, the code above doesn't work. Does any one know how I can display the output of the script "display.output.php" in the script "javascript.php"?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You have to escape all double quotes and replace newlines with \n in the output of display.output.php before plugging it into the javascript.
    Saul

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    include

    Let me see if I get this.
    You want to have PHP include a PHP file that outputs javascript that writes a PHP include of another PHP file?
    I imagine there's a problem with the PHP parsing the code only once so that the PHP the js writes is too late to get parsed (javascript is client-side).
    Maybe wrap the main page in an output buffer? Or maybe a less convulted way to get the same effect?

  4. #4
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    You have to escape all double quotes and replace newlines with \n in the output of display.output.php before plugging it into the javascript.
    Yeah, but how do I do this? Keep in mind that the output is generated by the script "display.output.php".
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  5. #5
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Maybe wrap the main page in an output buffer? Or maybe a less convulted way to get the same effect?
    Say what?
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    possible solutions

    I was wondering if you buffered the page's output, it might "give the parser" time to process the include (maybe).
    But I really think the answer is to rethink how to achieve the result you want.
    eg. have the include output the mark-up as a variable and pass that to the javascript. eg.
    PHP Code:
    <?php
    $output 
    "<div>blah blah</div>";
    ?>
    <?php
    include "blah_div.php";
    ....
    document.write('<?php echo $output; ?>');
    ...
    Although as php_daemon said, the real problem may be the quotes and newlines present in the include. You could test by using a simple include that doesn't have quotes or newlines. Something like
    PHP Code:
    <?php
    <div>This is a test</div>
    ?>
    <?php
    ....
    document.write('<?php addslashes(@include "display.output.php");?>');
    ....
    If that works then you could add str_replace to the addslashes and you should be all set.

  7. #7
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to give a variable the "value" of the output in some sort of way? Something like this $output = @include("display.output.php"); ??
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    file contents

    Instead of using include, you could do
    PHP Code:
    $handle fopen($path_file'r');
    $fs filesize($path_file);
    $file_data fread($handle$fs);
    fclose($handle);
    clearstatcache(); 
    The variable $file_data will contain the contents of the file as a string;

  9. #9
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Instead of using include, you could do
    PHP Code:
    $handle fopen($path_file'r');
    $fs filesize($path_file);
    $file_data fread($handle$fs);
    fclose($handle);
    clearstatcache(); 
    The variable $file_data will contain the contents of the file as a string;
    Won't this only work if the file only contains HTML? The file also contains PHP commands in it, so I don't think reading the file will work.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    file content

    If the file has PHP commands in it then I'd put the results into a variable and pass that to the js like I showed in my earlier post.
    Or have the include output the javascript tags/code then you just have to include it.

  11. #11
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I've decided to change my method. I will buffer the output of the script "display.output.php" in a variable called $output. This is the easiest way to do it.

    But how do I assign the following output to the variable $output without making change to the code?

    PHP Code:
    ?>
    <table bgcolor="<?php echo $bgcolor1?>" border="0" cellspacing="0" cellpadding="0" width="<?php echo $width?>">
    <tr><td><table border="<?php echo $border?>" cellspacing="<?php echo $cspace?>" cellpadding="2" width="100%">
    <tr bgcolor="<?php echo $bgcolor1?>"><td align="center" style="color:<?php echo $txtcolor1?>; font-family:<?php echo $fontface1?>; font-size:<?php echo $fontsize1?>; font-style:<?php if ($fontstyle1 == "i" or $fontstyle1 == "bi") {echo "italic";} else {echo "normal";} ?>; font-weight:<?php if ($fontstyle1 == "b" or $fontstyle1 == "bi") {echo "bold";} else {echo "normal";} ?>">
    <?php
    I've tried the following but it doesn't really work:

    PHP Code:
    $output = "
    ?>
    <table bgcolor="<?php echo $bgcolor1?>" border="0" cellspacing="0" cellpadding="0" width="<?php echo $width?>">
    <tr><td><table border="<?php echo $border?>" cellspacing="<?php echo $cspace?>" cellpadding="2" width="100%">
    <tr bgcolor="<?php echo $bgcolor1?>"><td align="center" style="color:<?php echo $txtcolor1?>; font-family:<?php echo $fontface1?>; font-size:<?php echo $fontsize1?>; font-style:<?php if ($fontstyle1 == "i" or $fontstyle1 == "bi") {echo "italic";} else {echo "normal";} ?>; font-weight:<?php if ($fontstyle1 == "b" or $fontstyle1 == "bi") {echo "bold";} else {echo "normal";} ?>">
    <?php
    ";
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    changes

    I understand about not wanting to change the code, but I think it may be more about making the least amount of changes rather than none at all if you want to use the code differently.
    May I ask why you don't simply include the mark-up instead of having javascript write it? If it's because you only want javascript enabled users to see it, you could use javascript to change CSS. Maybe something like
    PHP Code:
    <div name="from_output" style="display: none">
    <?php
    include 'display.output.php';
    ?.
    </
    div>
    <
    javascript ...
    ....
    .... 
    displayblock;


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
  •