【问题标题】:white space occur in textarea文本区域中出现空白
【发布时间】:2016-02-04 13:00:52
【问题描述】:

我正在尝试使用 php 和数据库更新查询并在页面中显示文本区域。这是我的代码,问题是我得到带有一些空格的文本区域。请帮忙。

if (isset($_POST["events"])&&isset($_POST["description"]))
{
    $id=$_POST["id"];
    $title=$_POST["events"];
    $description=$_POST["description"];
}

HTML

<form action="hotel1_galery_eventedit.php" method="post" class="col-sm-4" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo $val->event_id;?>">
    <div class="form-group has-info">
        <label class="control-label" for="inputSuccess">Event title</label>
        <input type="text" class="form-control" name="events" value="<?php echo $val->event_title;?>">
    </div>
    <div class="form-group has-info">
        <label class="control-label" >Event Description</label>
    <textarea name="description" class="form-control text-left" rows="3">
    <?php echo $val->event_description;?>
    </textarea>
    </div>
</form>

【问题讨论】:

  • 可能是换行符增加了空格..
  • &lt;textarea name="description" class="form-control text-left" rows="3"&gt;&lt;?php echo trim($val-&gt;event_description);?&gt;&lt;/textarea&gt; 如果它仍然产生空间,那么$val-&gt;event_description 中会有&amp;nbsp&lt;br&gt;

标签: javascript php html database


【解决方案1】:

在条件下使用 trim(),如果文本区域中的值是空白的,那么它将被视为 NULL 或者你可以使用 ''

if(isset($_POST["events"])&&isset($_POST["description"])){
            $id=$_POST["id"]; 
            $title=$_POST["events"];
            $description= ($_POST["description"] == '') ? NULL : trim($_POST["description"]);
    }

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
  • 感谢 Drenmi 分享有关堆栈规则的信息。
【解决方案2】:

将您的文本区域代码保持在同一行,在文本区域内输入会导致此问题。

<textarea name="description" class="form-control text-left" rows="3"><?php echo $val->event_description;?></textarea>

您在回显值之前添加的回车是造成这种情况的原因。

【讨论】:

    【解决方案3】:
    if (isset($_POST["events"])&&isset($_POST["description"])) 
                {
                    $id=mysql_real_escape_string(trim($_POST["id"])); 
                    $title=mysql_real_escape_string(trim($_POST["events"]));
                    $description=mysql_real_escape_string(trim($_POST["description"]));
                  }
    

    这是一种保护我们的数据库免受意外问题的安全方法,您还可以在没有任何额外空间的情况下显示数据...

    【讨论】:

      【解决方案4】:

      使用trim()

      if (isset($_POST["events"])&&isset($_POST["description"])) 
                      {
                          $id=$_POST["id"]; 
                          $title=trim($_POST["events"]);
                          $description= trim($_POST["description"]);
                      }
      

      【讨论】:

        【解决方案5】:

        您是否尝试过trim() 功能。像这样使用

        <textarea name="description" class="form-control text-left" rows="3">
        <?php echo trim($val->event_description);?>
        </textarea>
        

        希望这会对你有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-02
          • 2023-03-15
          • 2020-12-20
          • 2015-09-08
          • 2013-06-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多