html textarea rendering

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Armitage
Forum Newbie
Posts: 5
Joined: Tue Aug 14, 2007 12:32 pm

html textarea rendering

Post by Armitage »

Hey folks,

I've written a little php script that allows me to edit the files on the server.
The script provides me with a textbox for executing server side commands
and a text box for a filename. There's two buttons for opening the file and saving the file
to and from a textarea underneath.

Alas when I attempt to save something like the following small html snippet as a file :-

Code: Select all

<html>
	<body>
		<form action="cmdline.php" method="post">
	          <textarea>
        	     The code for the file goes in here. 
	          </textarea>
		</form>
	</body>
</html>
the end of the file i.e. from </textarea> down is no longer part of the html snippet and in fact the code

Code: Select all

</textarea>
		</form>
	</body>
</html>
is used to render the end of the editing textarea window??
I'm going a little mad trying to figure out why. i'm a newbie to php so if I've missed something obvious say the word.

The php code from the script that renders the textarea section is shown below and is actually the end of the file.

Code: Select all

:
:
:
               <textarea id="code" width="100%" height="400px" style="width: 100%; height: 600px" name="code" wrap="logical" rows="20" cols="80"><?php
			
OUTPUTTING THE FILE is done in HERE
			
		?></textarea>
		</form>
	</body>
</html>
Any help is greatly appreciated.
If anyone knows of any free php editors like this and they could point me in their direction that would be great.

Thanks,
Mark.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

When your PHP code if executed, it writes out the HTML snippet to the browser, which then sees it as valid HTML, so it renders it. What you can do is replace all < tags inside your HTML snippet with < (the special code version of the <). What this will do is inside the textarea, it will still display as <, but in terms of HTML, it will only see < and know that it is not a real tag. When you save it back to the file, you need to replace < with < so it saves it as real HTML for later display in the browser.
Armitage
Forum Newbie
Posts: 5
Joined: Tue Aug 14, 2007 12:32 pm

Post by Armitage »

Legend Moose!!

I've tried that swap before but not at the point where it's writing it to and from the file.
So basically when reading from the post change all the < and > to their < and > equivalent
and when writing it back to the file I need to undo it?

Sounds like a sed equivalent is required. I'll go and have a look for it now.

Thanks again,
Mark.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Always use htmlspecialchars() when putting data into a form element. No 'undoing' will be needed, as HTML processes them as their decoded counterparts.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

superdezign wrote:Always use htmlspecialchars() when putting data into a form element. No 'undoing' will be needed, as HTML processes them as their decoded counterparts.
Can't tell you how long I searched for the name of the function, I couldn't remember it for the life of me.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

TheMoose wrote:
superdezign wrote:Always use htmlspecialchars() when putting data into a form element. No 'undoing' will be needed, as HTML processes them as their decoded counterparts.
Can't tell you how long I searched for the name of the function, I couldn't remember it for the life of me.
Hehe, really? There's also htmlentities().
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

superdezign wrote:
TheMoose wrote:
superdezign wrote:Always use htmlspecialchars() when putting data into a form element. No 'undoing' will be needed, as HTML processes them as their decoded counterparts.
Can't tell you how long I searched for the name of the function, I couldn't remember it for the life of me.
Hehe, really? There's also htmlentities().
Yeah, been working all day in C# fixing work stuff, haven't had a chance to take a break and get in some PHP time :P
Armitage
Forum Newbie
Posts: 5
Joined: Tue Aug 14, 2007 12:32 pm

Post by Armitage »

Folks,

Thanks a million for all your help. I'll give that htmlspecialchars a go and see how it runs for me.


THanks,
MArk.
Armitage
Forum Newbie
Posts: 5
Joined: Tue Aug 14, 2007 12:32 pm

Post by Armitage »

Folks,

That worked a charm!!! Thanks a million. I were wrackin' me brain.

Still can't get my head around the whole Posting thing. Especially when the files
posting back to itself.

Thanks again,
Mark.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Requests take a bit to get, but one thing that might help you along (or confuse you more.. depends :P) is that GET and POST requests are actually a part of the entire page request, which makes them a part of the entire page (response). So a page with posted data is different than the same page without posted data.
Armitage
Forum Newbie
Posts: 5
Joined: Tue Aug 14, 2007 12:32 pm

Post by Armitage »

Legend Superdezign. hopefully after a while I'll get used to it.
Gotta say, quickest forum responses ever!!

Thanks,
Mark.
Post Reply