Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Let user control some formatting in text area 2

Status
Not open for further replies.

alexisb

Programmer
Apr 5, 2001
100
US
I am working on a basic page to display data entered in a text area. If the user enters the text below, is there a way for my php page to use the html formatting entered?
<H2>Here's my heading</H2><br>More text here<br><b>I am done now.</b>

This would give the user some control over formatting. I understand that this is dangerous and that the user would have to be very careful entering data.

Thanks,
Alexis
 
If this text will be later on injected in an html page (outside the textarea), all the formatting will be visible. If you want to limit the formatting or omit it altogether, use strip_tags()
 
@alexisb

the main problem you will face is dodgy input by users. your whole site design may be thrown off by a user forgetting to close a <div> for example.

consider using any of the rich-text-editors that are freely available. I recall Jon Honeyball (PC Pro Magazine- UK) giving one of these a very good rating a couple of months ago. have a look in their archives or just google for "web rich text editor" (bet you end up with using these will ensure that the user's html is valid when posted back to your site.

as Vragabond says intimates: be careful with what tags you let through the gate. many user accessible CMS's limit input to only certain style tags like strong etc.
 
oops- misremembered. given that this is a php forum you are unlikely to be using the RTE from richtexteditor.org (which is aimed at ASP).
 
Thanks to you both. I will try the FCKEditor editor. There is only one user that needs to edit one piece of text but he wants to be able to format it. I want to give him that ability if possible. I'll let you know how it goes.
Regards,
Alexis
 
The editor works great. But now let's say I end up with code like this in my text field in the database:
<H2>Here's my heading</H2><br>More text here<br><b>I am done now.</b>

How do I display the data with my display page so it uses the tags, rather than just displaying them within the text? My code below just sees the tags as part of the text entered. Do I have to search for occurrences of "<" and ">" in the text and strip them out somehow?

for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$news = stripslashes($row['news']);
echo "<textarea cols=50 rows=3>$news</textarea><br><br>";
}
?>

Thanks,
Alexis
 
You need to echo out your text to something else than textarea. Textarea is a form control that allows input and edits of longer text and not something that should be used to show final presentation. Just put things in a div or something.
 
Thanks so much. I didn't realize this. I don't do a lot of this type of work. I used div and now I can see the formatting. One question, though - the reason I used the textarea was to allow for scrolling if the text is larger than what I want to show by default. Therefore, if I only want to show 10 lines by default, but the user enters 20 lines, 10 lines would show and people reading the text would have to scroll to see the additional 10 lines. Is there a way to do this without using a textarea?

Thanks,
Alexis

 
That would be a design question which would have nothing to do with php at all. You would do it by aditionally styling your div via css and giving it a set width, height and set its overflow to auto. More help on that can be found at forum215.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top