【问题标题】:Why am I my losing my <textarea> data in the $_POST? [closed]为什么我丢失了 $_POST 中的 <textarea> 数据? [关闭]
【发布时间】:2012-08-29 21:29:44
【问题描述】:

我正在编写一个脚本,它将获取一些表单数据并以 PDF 或其他可打印格式呈现。表格底部是一个“新行”按钮。当您单击此按钮时,表单中会出现一个新行,并且上一行中的数据将添加到根据其标签标记命名的数组中。我的问题是,当提交表单时,最终在 $desc[] 数组中的唯一数据是最后一行中输入的数据。有人可以看看我的代码,看看我做错了什么吗?

if (isset($_POST['new_line']) || isset($_POST['submit'])) {

    // Grab POST data
    $invoice_quote = trim($_POST['invoice_quote']);
    $sales_person = trim($_POST['sales_person']);
    $job = trim($_POST['job']);
    $due_date = trim($_POST['due_date']);
    $first_name = trim($_POST['first_name']);
    $last_name = trim($_POST['last_name']);
    $address = trim($_POST['address']);
    $city = trim($_POST['city']);
    $state = trim($_POST['state']);
    $zip = trim($_POST['zip']);
    $phone = trim($_POST['phone']);
    $email = trim($_POST['email']);
    $qty = array_filter(array_map('trim', $_POST['qty']));
    $desc = array_filter(array_map('trim', $_POST['desc']));
    $unit_price = array_filter(array_map('trim', $_POST['unit_price']));
    $line_total = array_filter(array_map('trim', $_POST['line_total']));
    $line = trim($_POST['line']);



    if (isset($_POST['submit'])) {
        $flag = 'FALSE';

        // This is only for test proposes, This is untimately where the 
        // code will go that will generate the PDF
        echo ('<div class= "info">'.$invoice_quote.' '.$sales_person.' '.$job.' '.$due_date.' '.$first_name.' '.$last_name.' '.$address.' '.$city.' '.$state.' '.$zip.' '.$phone.' '.$email.'<br />');
        var_dump($qty);
        echo '<br />';
        var_dump($desc);
        echo '<br />';
        var_dump($unit_price);
        echo '<br />';
        var_dump($line_total);
        echo('</div>');
        exit();

      }

}

<form id="invoiceform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset class= "description">
                            <label for= "qty" class= "qty">Qty</label>
                            <label for= "desc" class= "desc">Description</label>
                            <label for= "unit_price" class= "unit_price">Unit Price</label>
                            <label for= "line_total" class= "line_total">Line Total</label>
                            <br/>
                    <?php
                        if ($flag == 'TRUE') {
                            $n = 0;
                            do {
                    ?>
                                <input type= "text" class= "qtyinfo" name= "qty[]" maxlength "10" value= "<?php if (isset($qty[$n])) echo ($qty[$n]); ?>"/>
                                <textarea class= "descinfo" name= "desc[]" id="<?php echo('desc_'.$n); ?>" value= "<?php if (isset($desc[$n])) echo ($desc[$n]); ?>"></textarea>
                                <input type= "text" class= "unit_priceinfo" name= "unit_price[]" id="<?php echo('unit_price_'.$n); ?>" maxlength= "10" value= "<?php if (isset($unit_price[$n])) echo ($unit_price[$n]); ?>" />
                                <input type= "text" class= "line_totalinfo" name= "line_total[]" id= "<?php echo('line_total_'.$n); ?>" maxlength= "10" value= "<?php if (isset($line_total[$n])) echo ($line_total[$n]); ?>" />
                                <br/>
                    <?php
                                $n++;
                            }
                            while ($n < $line);
                        }
                    ?>



                <input type= "hidden" id= "line" name= "line" value= "<?php echo $line + 1; ?>"/>
                <input type= "submit" id= "new_line" name= "new_line" value= "New Line" />
            </fieldset><br />

【问题讨论】:

  • 请减少您提供给我们的代码数量。包括表单标签、相关表单元素和与发布数据后访问数据相关的代码。其他一切都无关紧要(目前)。
  • var_dump($_POST) 向您展示了什么?
  • 您所说的按钮的实际代码在哪里?以及添加“新行”的代码?
  • 我已经对 PeterVR 建议的这段代码进行了一些更改。这些更改稍微清理了我的代码,但问题仍然存在。我也清理了我的帖子。我已经删除了脚本中所有不相关的部分。 j08691,你问“换行”按钮在哪里?这是这篇文章中的最后一个输入 type="submit"。 andrewsi,var_dump() 为 $desc[] 数组中的所有先前元素显示一个空白字符串。

标签: php html forms post


【解决方案1】:

试试看:

<textarea id= "desc" name=  "<?php echo('desc_'.$n); ?>"><?php if (isset($desc[$n])) echo ($desc[$n]); ?></textarea>

【讨论】:

  • 我给我的 textarea 标签一个值属性。我花了一段时间才意识到你告诉我要做什么,但我明白了。有时候我有点头晕
【解决方案2】:

您似乎正在添加具有相同 ID 的多个字段。在你的循环中,你有这个:

<input type= "text" id= "qty" name= "<?php echo('qty_'.$n); ?>" maxlength "10" value= "<?php if (isset($qty[$n])) echo ($qty[$n]); ?>"/>
<textarea id= "desc" name=  "<?php echo('desc_'.$n); ?>" value= "<?php if (isset($desc[$n])) echo ($desc[$n]); ?>"></textarea>
<input type= "text" id= "unit_price" name=  "<?php echo('unit_price_'.$n); ?>" maxlength= "10" value= "<?php if (isset($unit_price[$n])) echo ($unit_price[$n]); ?>" />
<input type= "text" id= "line_total" name=  "<?php echo('line_total_'.$n); ?>" maxlength= "10" value= "<?php if (isset($line_total[$n])) echo ($line_total[$n]); ?>" />

当循环运行不止一次时,这会产生相同 id 的多个实例,这是无效的 html,可以解释您遇到的问题。我建议做这样的事情:

    <input type="text" name="qty[]" id="<?php echo('qty_'.$n); ?>" maxlength="10" value="<?php if (isset($qty[$n])) echo ($qty[$n]); ?>"/>
    <textarea name="desc[]" id="<?php echo('desc_'.$n); ?>" value="<?php if (isset($desc[$n])) echo ($desc[$n]); ?>"></textarea>
    <input type="text" name="unit_price[]" id="<?php echo('unit_price_'.$n); ?>" maxlength="10" value="<?php if (isset($unit_price[$n])) echo ($unit_price[$n]); ?>" />
    <input type="text" name="line_total[]" id= "<?php echo('line_total_'.$n); ?>" maxlength="10" value="<?php if (isset($line_total[$n])) echo ($line_total[$n]); ?>" />

您会注意到我交换了名称和 ID。允许使用多个名称。更重要的是,如果你像我一样添加方括号,具有相同名称的字段将作为数组返回,这使得在后端处理它们非常容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2012-06-19
    • 1970-01-01
    • 2021-07-22
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多