Search code examples
phphtmlmysqlforum

how to keep the text indentation same as user written in the <textarea>


i am using php.my project is to build a forum...everything is fine and ok except onething...there is a <textarea> on top of my site,through which user can ask their question..now am using insert into command to insert whatever user has wriiten in textarea and select command to fetch the posts from database and the display it using echo command

problem : unable to keep the user's text indentation intact

suppose user has written

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
    ...and so on;
  }

this code in the text area,while am displaying it through echo then the output is like

#include<stdio.h>#include<conio.h>int main(){...and so on;}

so my question is how can i keep the text indentation of user intact

edit 1:my insertion code

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/include/db.inc.php' ;
$myemail=$z;
$sem=$_POST['semester'];
$course=$_POST['course'];
$title=$_POST['title'];
$desc=$_POST['question'];
$date=date('Y-m-d H:i:s');
$sem = mysqli_real_escape_string($link, $sem);
$course = mysqli_real_escape_string($link, $course);
$title = mysqli_real_escape_string($link, $title);
$desc = mysqli_real_escape_string($link, $desc);
$sql =  "INSERT INTO wbut_forum_posts SET post_title='$title',post_desc='$desc',course='$course',semester='$sem',post_by='$myemail',post_date='$date'";
if (!mysqli_query($link,$sql))
{
include_once "wall.html.php";
echo "<script language=javascript> alert(\"SOMETHING WENT WRONG, TRY AGAIN LATER !!!\");</script>"; 
exit();
}
else
{
include_once 'wall.html.php';
echo "<script language=javascript> alert(\"YOUR POST SUCCESSFULLY HAS BEEN POSTED\");        </script>";
exit();
}

Solution

  • Instead of using a straight text area, try a WYSIWYG editor like tinyMCE, after playing with the settings a bit you will be able to get it to create the HTML of what the users enter, so you can get line breaks to convert to <br> tags and tabs to convert to &nbsp;(x4) or x8 or whatever you want. You can also remove all the controls to so it won't necessarily look like a WYSIWYG editor.

    EDIT: WYSIWYG (What You See Is What You Get) - tinyMCE (http://www.tinymce.com/) is one such editor. You can follow the directions on their site, but it basically loads from JS and overrides your text areas with their editor. When the user clicks save or whatever (you tell tinyMCE via JS that the user is done) it will take their text as you see it and convert it to html, that converted text is what you would store in your DB. Since it's html being stored, all the line breaks and white spaces are preserved. A tool like this does require some JS skills to implement, try it and see what problems you come up against. This is kind of an overkill solution. But if you get tinyMCE properly set up it will give you complete control of what gets stored in your DB.