1 Replies - 944 Views - Last Post: 03 August 2014 - 09:22 AM

#1 ettedo2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-September 12

Store text in textarea with formatting and have correctly displayed ?

Posted 03 August 2014 - 05:24 AM

I have a web form that allows the user to type a introduction into a textarea box to be saved on the DB and also be displayed on their profile page. However, even though I place the text in the box with spaces and listed items, once stored in the DB all the formatting is lost and the text is displayed in one chunk of text in the profile field (all the formatting has been lost).

How can I change this so that the formatting is kept in the text when stored in DB and displayed in the profile page. I tried to implement html tags in the text but it did not make a difference.

Also I would love to have a simple edit tool box included in the textarea, where the user can make there text bold, italic ect. Is that possible?

I do use php in my code but I really have no idea where to find information on this or what is possible.
Thank you

I added my code to show you what I mean.

  <form action="" method="post">
    <table width="550" border="0" class="form_field" >
      <td width="228">Username:</td>
      <td width="391"><?php echo escape($user->data()->username); ?></td>
  <td><label for="first_name">First Name: </label></td>
    <td><?php echo escape($user->data()->first_name); ?></td>
        <td><label for="last_name">Last Name:</label></td>
        <td><?php echo escape($user->data()->last_name); ?></td>
    <td><label for="city">City :</label></td>
    <td> <input name="city" type="text" id="city" size="40" value="<?php echo escape($user->data()->city); ?>"></td>
    <td valign="top"><label for="intro">Introduction:</label></td>
    <td><textarea name="intro" cols="55" rows="10" id="intro" type="text" ><?php echo escape($user->data()->intro); ?></textarea><br/><h5>Write a short Introduction about yourself <br/>
    max: 1000 charachters</h5></td>
    <td><label for="edu">Education:</label></td> 
<td><input name="edu" type="text" id="edu" size="40" value="<?php echo escape($user->data()->edu); ?>" ></td>
    <td width="228"><label for="proff">Proffession :</label></td>
    <td width="391"><input name="proff" type="text" id="proff" size="40" value="<?php echo escape($user->data()->proff); ?>" ></td>
    <td valign="top"><label for="accomp">Accomplishment: </label></td>
    <td><textarea name="accomp" cols="55" rows="15" id="accomp" type="text"><?php echo escape($user->data()->accomp); ?></textarea><br/><h5>Write about your Professional and Personal <br/>
    Accomplishments max: 5000 charachters</h5></td>
      <td height="45" align="center"><input type="submit" value="Create/Update Profile"></td>
      <td><input type="hidden" name="token" value="<?php echo Token::generate(); ?>"></td>

Is This A Good Question/Topic? 0
  • +

Replies To: Store text in textarea with formatting and have correctly displayed ?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5226
  • View blog
  • Posts: 14,005
  • Joined: 18-April 07

Re: Store text in textarea with formatting and have correctly displayed ?

Posted 03 August 2014 - 09:22 AM

Well first of all you are going to have to include HTML tags in the textarea box. Textarea is for straight text, no styling or anything. Just like a textbox is. That means you are going to have to add tags like <div> or <span> tags with css styling preferably and save that to your database tags and all. Then when you read the data back out from the database and render it on the page, the HTML tags will then format the content when you show it on page.

So for instance... let's assume you would like some text to come out red. In the textarea you will have to enter something like... <span style="color: #ff0000;">My red text</span> (be sure to escape this properly before entering it into the database). Then when you go to show the content on the page, you read it back out, tags and all, and then it will show the text styled as red.

One last note that when you go to show the text, make sure you also encode it for entities otherwise you can end up with a cross site scripting vulnerability (people able to render things like javascript without your control).

I hope you get the idea. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1