dotelauv682684 2013-04-02 09:45
浏览 112
已采纳

如何保持文本缩进与<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();
}

展开全部

  • 写回答

5条回答 默认 最新

  • duanmao1919 2013-04-02 11:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部