search

Home  >  Q&A  >  body text

php - 为什么在textarea中显示编辑内容时,开头总有6个多的空格

在sina app engine上用php写自己的博客程序。 在创建或更新文章时,用 textarea 来容放文章内容, 暂时以纯文本的形式将文章内容保存到数据库。

表单是这样的(在write_article.php中):

<form action="save_article.php?id=<?php echo $id; ?>" method="post" align="center">
  ... ...
  <p>
  <textarea rows="20" cols="80" name="article_text">
  <?php if($op == "update"){echo $content;} ?></textarea></p>
  <p><input type="submit" value="提交"/></p>
</form>

保存文章的sql语句是这样的(在save_article.php中)。

$content = $_POST["article_text"];
// update article
$sql = "update article set title=\"$title\",type=\"$category\",content=\"$content\",summary=\"$summary\",updatetime=\"$datetime\" where id=$id";
... ...
// save new article
$sql = "insert into article values ($id, \"$category\", \"$title\", \"$author\", \"$summary\", \"$content\", \"$datetime\", \"$datetime\", $hits)";

但是更新文章时(在write_article.php中进行), 发现在textarea中显示的文本开头总是多出六个空格。 删除掉空格,再提交文章。 等到再编辑文章时,发现textarea开头还是多出六个空格。

但在 阅读文章 时,发现文章开头并没有多出的空格。

百思不得其解,所以向各位求助,这多出的空格是怎么回事呢?

大家讲道理大家讲道理2901 days ago435

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-04-10 14:36:01

    <textarea>后面有空格和换行,所以会这样。内容顶着<textarea>写,

    保存的时候用 $content =trim( $_POST["article_text"] );

    就可以了~

    reply
    0
  • 迷茫

    迷茫2017-04-10 14:36:01

    改成:

    <textarea rows="20" cols="80" name="article_text"><?php if($op == "update"){echo $content;} ?></textarea>
    

    注意 textarea 中不要有任何多余的东西,你原来的代码里有换行和空格。

    reply
    0
  • PHPz

    PHPz2017-04-10 14:36:01

    我也遇到了 因为<textarea>标签里有换行
    写到同一行 <textarea.....>....内容.....</textarea> 就好了

    reply
    0
  • Cancelreply