php_begins Posted September 28, 2011 Share Posted September 28, 2011 I have page called display.php which has php code in it. I need a text area and a button in that page such that when I click on that button it displays the html source code of that page in the textarea. Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/ Share on other sites More sharing options...
ManiacDan Posted September 28, 2011 Share Posted September 28, 2011 PHP, HTML, and javaScript are entirely separate languages. Don't get them confused. It doesn't matter which language generated the page if all you care about is the HTML source. That being said, if someone wants to view the HTML source of your page, they have to press ctrl+U. That's all. Do it right now. If you really want it to show up in a textbox, I'm sure that you can use some variant of the innerHTML attribute on the entire document, then stick that into a textarea. I wouldn't recommend that though, it might screw up some browsers. The "view source" menu option built into all browsers is enough. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1273755 Share on other sites More sharing options...
php_begins Posted September 28, 2011 Author Share Posted September 28, 2011 well my site administrator needs to enter a set of urls and image urls in a form. When he clicks the submit button, display.php creates a web page based on the information he entered. He wants the source code in a text area. Since i am not proficient in javascript, I would be grateful if someone could help me out with the code... Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1273756 Share on other sites More sharing options...
ZulfadlyAshBurn Posted September 29, 2011 Share Posted September 29, 2011 Its pretty simple? can you show you current code? I can help you with it. edit: can be easily done with php Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1273885 Share on other sites More sharing options...
php_begins Posted September 29, 2011 Author Share Posted September 29, 2011 well the page is called display.php..so whatever code is on that page(php or html) needs to be converted into html(like view source) and be displayed in a textarea field... Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1274000 Share on other sites More sharing options...
ManiacDan Posted September 29, 2011 Share Posted September 29, 2011 well the page is called display.php..so whatever code is on that page(php or html) needs to be converted into html(like view source) and be displayed in a textarea field... Again, stop confusing PHP with HTML. PHP is a language that generates (for the most part) text output. PHP can be used to generate novels, emails, invoices, OR html documents. PHP doesn't care what it's generating and whatever is generated doesn't care that PHP was involved. You are not at all interested in the PHP side of this equation. At all. Really, you're not. This is entirely an html/javascript question. document.getElementById('idOfYourTextAreaHere').innerHTML = document.documentElement.innerHTML; That's all you have to do. Run that javascript at the bottom of the page (or put it into a button) and as long as you have a textarea on the page with the proper ID then it will be filled with the page's entire content (minus the <html> and </html> tags and any doctype declarations). I don't know why you need this rather than the "view source" that everyone else uses, but there's the solution. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1274070 Share on other sites More sharing options...
php_begins Posted September 29, 2011 Author Share Posted September 29, 2011 well the page is called display.php..so whatever code is on that page(php or html) needs to be converted into html(like view source) and be displayed in a textarea field... Again, stop confusing PHP with HTML. PHP is a language that generates (for the most part) text output. PHP can be used to generate novels, emails, invoices, OR html documents. PHP doesn't care what it's generating and whatever is generated doesn't care that PHP was involved. You are not at all interested in the PHP side of this equation. At all. Really, you're not. This is entirely an html/javascript question. document.getElementById('idOfYourTextAreaHere').innerHTML = document.documentElement.innerHTML; That's all you have to do. Run that javascript at the bottom of the page (or put it into a button) and as long as you have a textarea on the page with the proper ID then it will be filled with the page's entire content (minus the <html> and </html> tags and any doctype declarations). I don't know why you need this rather than the "view source" that everyone else uses, but there's the solution. -Dan Thanks, sometimes you have to do what your boss tells you to do...so i m breaking my head over trying to do this with javascript(unfamiliar territory for me) Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1274139 Share on other sites More sharing options...
php_begins Posted October 6, 2011 Author Share Posted October 6, 2011 I am still not able to get the html code of the current page..i am having some trouble in calling the javascript part. Is this correct. <textarea id='code'><script type='text/javascript'>document.getElementById('code').innerHTML = document.documentElement.innerHTML;</script></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276559 Share on other sites More sharing options...
php_begins Posted October 6, 2011 Author Share Posted October 6, 2011 I even tried this: <script type="text/javascript" language="javascript"> function dump() { document.getElementById('debug').value = document.innerHTML; } </script> <textarea id='debug'></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276564 Share on other sites More sharing options...
ManiacDan Posted October 6, 2011 Share Posted October 6, 2011 The first one is wrong because the JS is inside the textarea when it should be outside. The second is wrong because you used value instead of innerHTML and you're not calling this function anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276593 Share on other sites More sharing options...
php_begins Posted October 6, 2011 Author Share Posted October 6, 2011 I am still struggling a bit..here's what i tried. <script type="text/javascript" language="javascript"> function dump() { document.getElementById('debug').innerHTML = document.innerHTML; } </script> <form action="#" name="form1"> <fieldset><legend>form</legend> <textarea rows="10" cols="20" name="textarea1" id="debug"> </textarea> <button type="button" onclick="dump()">Get CODE</button> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276600 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 For the love of god, copy and paste. Every time you show a new example you've subtly altered the code I've given you. Why does your code not match the code I gave you? the code I gave you worked. Replacing your JS function with the actual line I gave you works in chrome: <script type="text/javascript" language="javascript"> function dump() { document.getElementById('debug').innerHTML = document.documentElement.innerHTML; } </script> <form action="#" name="form1"> <fieldset><legend>form</legend> <textarea rows="10" cols="20" name="textarea1" id="debug"> </textarea> <button type="button" onclick="dump()">Get CODE</button> </fieldset> -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276648 Share on other sites More sharing options...
ManiacDan Posted October 7, 2011 Share Posted October 7, 2011 Also, there's absolutely no reason to have a form here. Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276649 Share on other sites More sharing options...
php_begins Posted October 7, 2011 Author Share Posted October 7, 2011 Thank you,Sorry for creating all the confusion.. Quote Link to comment https://forums.phpfreaks.com/topic/248065-display-html-code-on-button-click/#findComment-1276879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.