Textarea不在表单中发布值

问题描述

我找到了许多有关类似问题的解决方案,但不幸的是,没有一个解决方案可以解决我的问题。除文本区域外,该表格工作正常。表单不发布textarea值,而是显示

未定义索引:说明

这是HTML代码

<form action="<?PHP echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 
 <div class="form-group">
                    <label>Description</label>
                    <textarea name="description" rows="4" cols="50" class="form-control" value=""></textarea>
 </div>
</form>

PHP

$description = $_POST['description'];

有人知道问题出在哪里吗?预先感谢。

解决方法

始终确保在使用button函数访问表单字段的值之前,先测试是否已单击提交isset。这是因为在服务器加载页面时,服务器会尝试检索尚未输入的输入字段的值。这样的undefined。另一个解决方案是使用另一个页面来处理您的表单提交。

    <?php
    if(isset($_POST['submit'])){
     $age = $_POST['age'];
     $description = $_POST['description'];
    }
    ?>
      <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group">
          <input name="age" />
          <label>Description</label>
          <textarea name="description" rows="4" cols="50" class="form-control" value=""></textarea>
          <button name="submit" value="save">Save</button>
        </div>
      </form>