You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP:使用MPDF导出多个textarea内容仅导出最后一个的问题求助

问题原因

所有<textarea>用了相同的name="editor"和id="editor",表单提交时同名元素只会传递最后一个的值到后端,导致只能导出最后一个文本框的内容。同时所有导出按钮在同一个表单里,点击任意按钮都会提交整个表单,没法区分要导出的目标文本框。

解决方案

方案1:每个文本框配独立表单

把循环里的每个<textarea>和导出按钮放进单独的<form>标签,点击每个导出按钮时只会提交对应文本框的内容:

修改后的表单代码

<?php
while ($rows = mysqli_fetch_assoc($result)) {
?>
<form method='post' action='thanks.php' enctype="multipart/form-data">
  <textarea name="editor" id="editor_<?= $rows['id']; ?>">
    <?= $rows['Content']; ?>
  </textarea><br>
  <input type="submit" name="export" value="Export" class="btn third">
</form>
<?php
}
?>

注:给每个textarea设置唯一id(比如用数据库行的id),避免DOM元素id重复的问题。

方案2:单个表单+数组名+区分导出目标

如果想保留单个表单,需要把textarea的name改成数组形式,给每个导出按钮加自定义属性标记对应文本框的索引,再用JS控制提交的内容:

修改后的表单代码

<form method='post' action='thanks.php' enctype="multipart/form-data" id="export-form">
  <?php
  $index = 0;
  while ($rows = mysqli_fetch_assoc($result)) {
  ?>
  <textarea name="editor[]" id="editor_<?= $index; ?>">
    <?= $rows['Content']; ?>
  </textarea><br>
  <input type="button" name="export" value="Export" class="btn third export-btn" data-index="<?= $index; ?>">
  <?php
  $index++;
  }
  ?>
  <!-- 隐藏域用来传递要导出的索引 -->
  <input type="hidden" name="target_index" id="target_index">
</form>

<script>
document.querySelectorAll('.export-btn').forEach(btn => {
  btn.addEventListener('click', function() {
    const index = this.getAttribute('data-index');
    document.getElementById('target_index').value = index;
    document.getElementById('export-form').submit();
  });
});
</script>

修改后的后端导出代码

require_once __DIR__ . '/vendor/autoload.php';

if (isset($_POST['editor'], $_POST['target_index']) && !empty($_POST['editor'][$_POST['target_index']])) {
    $targetIndex = intval($_POST['target_index']);
    $pdfcontent = $_POST['editor'][$targetIndex];

    $mpdf = new \Mpdf\Mpdf();
    $mpdf->WriteHTML($pdfcontent);
    $mpdf->Output('mypdf.pdf', 'D');
}

额外需求:一次性导出所有文本框内容

如果需要导出全部textarea的内容,只需把textarea的name设为editor[],后端循环遍历数组内容写入PDF:

表单代码(仅修改name)

<form method='post' action='thanks.php' enctype="multipart/form-data">
  <?php
  while ($rows = mysqli_fetch_assoc($result)) {
  ?>
  <textarea name="editor[]" id="editor_<?= $rows['id']; ?>">
    <?= $rows['Content']; ?>
  </textarea><br>
  <?php
  }
  ?>
  <input type="submit" name="export_all" value="Export All" class="btn third">
</form>

后端导出代码

require_once __DIR__ . '/vendor/autoload.php';

if (isset($_POST['editor']) && is_array($_POST['editor']) && !empty($_POST['editor'])) {
    $mpdf = new \Mpdf\Mpdf();
    
    foreach ($_POST['editor'] as $content) {
        if (!empty(trim($content))) {
            $mpdf->WriteHTML($content);
            $mpdf->AddPage(); // 每个内容单独一页,可按需调整
        }
    }

    $mpdf->Output('all_content.pdf', 'D');
}

内容的提问来源于stack exchange,提问作者Rohan Chooramun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 15:30:21