Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    10
    Rep Power
    0

    Have PHP display actual JavaScript code instead of run it?


    I am trying to make a form that will generate HTML and JavaScript code people can then cut and paste and use on their own Web sites.

    The user inputs the necessary information and then submits the form, so far so good, and I know how to make the HTML and JavaScript code based on the variables. What I CAN'T figure out is how to make the php page display the actual HTML and Javascript code verbatim (showing all the tags, etc.) instead of parsing the code and running it.

    Suggestions most welcome!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    14
    Lets say you want to display:
    <script language="javascript"> .

    In html, to display that, you would have to write:
    &lt;script language="javascript"&gt;

    Using php to echo the html:
    echo "&lt;script language=\"javascript\"&gt;";

    Hope this helps
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Or, display it in a text area.
    PHP Code:
    <html>
    <body>
    <textarea name="ta1" rows="10" cols="50">

    <?php
    echo "<div>Hello World</div>\n";
    echo 
    "<script type=\"text/javascript\">\n";
    echo 
    "alert(\"Goodbye\");\n";
    echo 
    '</script>';
    ?>

    </textarea>
    </body>
    </html>
    Last edited by 7stud; September 1st, 2001 at 10:58 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    Have you tried storing the javascript code in a string - $code, running it through htmlspecialchars() and then doing a preg_replace("/\r\n/","<br>",$code) and finally printing $code out to the browser?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Uhhh jacobee,

    If you send html or javascript to the browser it executes the code. Running it through any number of functions first will not keep the browser from executing the html that reaches the broswer. Your suggestion turns some of the non-html characters into html. Why do you think the browser is NOT going to execute those <br> tags?
    Last edited by 7stud; September 2nd, 2001 at 02:07 AM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    htmlspecialchars() turns the &lt and &gt into the appropriate html entities(&amplt and &ampgt), not the other way around. This would prevent any javascript or html from being parsed as such and instead be printed out as plain text. My suggestion for running the $code string through the preg_replace("/\r\n/","<br>",$code) would replace the carriage return/new line characters that wouldn't affect the html output with &ltbr&gt tags so that you can see the line breaks in the javascript code.
    Last edited by jacobee; September 2nd, 2001 at 02:13 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    Well those <br> tags you are putting in the string aren't being run through html special characters are they? Furthermore, if I am a client of the poster, and I cut and paste your new string into my website, is it going to display the html?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Ok, I see what you are doing with the \r\n and <br> tags. Will someone cut and pasting the new string into their browser be able to have the html entities execute as html?
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    You add the &ltbr&gt after running through htmlspecialchars() otherwise you would end up displaying the line break tags as &ltbr&gt instead of actually inserting a break into the html flow. If you were to cut and paste the output of what I propose into a .js file for inclusion in an html document (or inside a set of &ltscript&gt&lt/script&gt for that matter) it shoud run as javascript code because you're not copying any of the underlying HTML markup, just the text which is valid javascript. Does that make sense?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Ahhh...I think I understand now. Will the html entities display like html, but they don't execute?
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    For good measure you might want to stick the modified $code string inside of &ltpre&gt&lt/pre&gt in order to preserve any indenting and whatnot. What do you mean by "display like html"?
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    If you cut and pasted the output of what we're discussing you would get plain text with no html entities present.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Like the first reply said:

    If you want this html to literally be displayed:

    <script language="javascript">

    then you send the html entity version to the browser?

    I think that must be it. Ok, then I have one suggestion to your suggestion, use str_replace() instead of the regexp, it's faster. Thanks for the lesson!
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    MN
    Posts
    7
    Rep Power
    0
    I guess I'm just used to PERLs regex format, but I think you're right that str_replace() would be faster and more straightforward as well. If the original input that made up the $code variable from above include the &ltscript type='text/javascript'&gt&lt/script&gt tags then it would display as if you were reading the source file in the browser as opposed to not being able to see the opening and closing source tags.
    Thanks for the discussion - it's always fun.
    Last edited by jacobee; September 2nd, 2001 at 02:29 AM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    I just tried it out. Thanks, that's pretty neat.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo