【问题标题】:can't keep data of textarea in an html form无法将 textarea 的数据保存在 html 表单中
【发布时间】:2014-03-15 17:29:19
【问题描述】:

我使用 php 从 html5 表单中获取数据,我正在尝试保留字段的数据并且不被清除。

它适用于输入,但对于 textarea(描述字段)失败。

您可以在下面看到我在输入中添加了一个 php echo 作为值,但它不适用于 textarea。 我怎样才能使它也适用于 textarea?

HTML 表单:

<form method='post' action='entry.php' enctype="multipart/form-data">
<label for="username"> Username: </label>
<input type="text" name="username" id="username" value="<?php echo $username; ?>" /></br>
</br>
<label for="email">Email:</label> 
<input type="email" name="email" value="<?php echo $email; ?>"/> </br></br>
<label>Title of monument:</label>
<input type="textbox" name="title" value="<?php echo $title; ?>" /></br></br>
<label>Description of monument:</label>

<textarea cols="50" rows="6" name="description" value="<?php echo $description; ?>" /></textarea></br></br>
<label>Select image:</label>
<input type="file" name="file"  ></br></br>
<input type="submit" value="Submit" name="submit" > 
</form> 

【问题讨论】:

  • 失败怎么办?请澄清。
  • 您有很多换行元素缺少(强制)开始标签但有(禁止)结束标签! (无论如何,您不应该使用换行符来模拟边距)。

标签: php html forms echo


【解决方案1】:

替换

textarea cols="50" rows="6" name="description" value="<?php echo $description; ?>" ></textarea>

<textarea cols="50" rows="6" name="description"  ><?php echo $description; ?></textarea>

我在 textarea 元素中添加了 php echo,并从 textarea value 属性中删除了它。 它应该可以完成工作。

【讨论】:

    【解决方案2】:

    textarea 元素的值由其内部的文本节点决定,而不是由 value 属性决定。

    <textarea cols="50" rows="6" name="description"><?php
      echo htmlspecialchars($description);
    ?></textarea>
    

    【讨论】:

      【解决方案3】:

      Textareas 没有 value 属性。

      相反,您应该将值直接放在&lt;textarea&gt;&lt;/textarea&gt; 标记之间。

      像这样:

      <textarea cols="50" rows="6" name="description"><?php 
            echo htmlspecialchars($description);
      ?></textarea>
      

      你还需要通过htmlspecialchars()传递$description,以防$description中的值包含像其他&lt;textareas&gt;这样的HTML标签。

      【讨论】:

        【解决方案4】:

        你已经关闭了 textarea 标签:

        <textarea cols="50" rows="6" name="description"  />
        

        需要去掉右斜杠,这样你的 php 就会被插入到 textarea 中!

        <textarea cols="50" rows="6" name="description" >     <?php ... ?>     </textarea>
        

        【讨论】:

        • / 是语法错误,不会关闭它(xhtml 除外,但问题标记为html)。
        猜你喜欢
        • 2018-03-16
        • 1970-01-01
        • 2015-06-14
        • 2021-08-25
        • 2017-10-27
        • 2014-11-19
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        相关资源
        最近更新 更多