Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I basically just want to append a javascript variable to an HTML div with JQuery. However I need to append some PHP code as a string, it doesn't need to execute it just needs to show up as a plain old string string.

The following code doesn't seem to append because I think it is still recognized as PHP syntax.

var script = '<?php wp_list_pages(); ?>';
divName.innerHTML = script;

Any help would be much appreciated.

share|improve this question
    
Can you elaborate on what exactly you need to do? –  Matteo Riva Feb 1 '10 at 16:34
    
I just want a div 'divName' to show some the contents of 'script' on a click. If script = 'test' it works fine. But not with the code above. –  Goldy Feb 1 '10 at 16:38
    
I hope you're only tring to display this PHP code. Please tell me you're not trying to pass it in a form or anything. Please, please. –  Lucas Oman Feb 1 '10 at 16:51
    
Ha no don't worry Lucas just wanted to display it as text in a browser that's all. –  Goldy Feb 1 '10 at 16:59
add comment

7 Answers

up vote 3 down vote accepted

Just a guess... replace the php brackets with HTML entities (&lt; and &gt;) so it will not be interpreted as PHP code (if you run the file containing the JS through PHP) nor as strange HTML code (the browser searches for brackets as html tags, remember...) by the browser.

share|improve this answer
    
Thanks this does the trick. Only problem now is I am going to use Adobe AIR to save the contents of that div as an actual PHP file, and therefore needs to execute in that file. Not sure what I am trying to achieve is actually possible. However your answer was useful. Thanks. –  Goldy Feb 1 '10 at 16:53
    
@Goldy NO! Please see my answer. –  Lucas Oman Feb 1 '10 at 17:02
add comment

Wrap it in CODE tags, like this:

var script = '<code><?php wp_list_pages(); ?></code>';
share|improve this answer
add comment

Try:

var script = '<?php echo '<?php wp_list_pages(); ?>'; ?>';

share|improve this answer
2  
People, I wouldn't be so quick to downvote this. The OPs descriptions allows the reading that this is exactly what he is looking for. –  Gordon Feb 1 '10 at 16:43
add comment

You probably should escape the "hot" HTML tokens in the PHP text:

div.innerHTML = script.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
share|improve this answer
    
This also does the works, thanks. –  Goldy Feb 1 '10 at 16:57
add comment

You need to simply escape the string with HTML entities (&lt; and &gt;)

IMPORTANT: I hope all you're doing here is trying to display the PHP code. Please don't try anything funky where PHP code is passed in a form's field back to the server and executed via eval() or somesuch. That would be an unimaginably terrible idea. Anytime you give the client access to code that will be executed on the server, you open yourself up to all kinds of exploits. Your server will be forfeit. Do not collect $200. Game over. Fail.

share|improve this answer
1  
heheh, that's the first thing that crossed my mind reading this :) –  Paolo Feb 1 '10 at 16:51
    
No need to worry, all I wanted to just show the code as text! Was not planning to try anything 'funky'. I am attempting to create a theme customizing tool with Adobe AIR. –  Goldy Feb 1 '10 at 17:02
    
@Goldy Ok. You really had me worried for a moment. Even if you understand what a terrible idea this is, you'd be amazed how often people cook up awful schemes like this. –  Lucas Oman Feb 1 '10 at 17:05
add comment

PHP is executed before the page is sent to the browser, so you can go to that page, open its source and see if in that line you see what you need to see, like:

var script = 'the content you expect to see';

if not, tell us what you see.

share|improve this answer
add comment

If you are looking for a String variable to execute PHP code try looking here instead. I mention this because this was what I was looking to do and this question came up.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.