【问题标题】:Creating HTML table with POST使用 POST 创建 HTML 表
【发布时间】:2020-12-10 04:40:25
【问题描述】:

我正在尝试创建一个网页,该网页接受用户输入,将其发布到创建页面,然后创建另一个网页,在 html 表中显示数据。我正在使用 file_put_contents 创建网页,每当我尝试包含一个循环以从 3 个 td 中获取要输出的内容时,我都会收到错误消息。这是我到目前为止所拥有的。我会提到我之前发布的内容,但是我删除了该帖子,因为该帖子令人困惑,缺乏细节和示例。

这是用户的输入字段(deleteBox 不会被发布) 用户将输入:成分、数量和备注(txt 为空变量)

<tr><td><input type='checkbox' name='deleteBox'></td><td>" + "<input type='text' name='ingredient[]' value='" + txt + "'/>" + "</td><td>" + "<input type='text' name='amount[]' value='" + txt + "'/>" + "</td> + <td>" + "<input type='text' name='note[]' value='" + txt + "'/>" + "</td></tr>

我已经在变量中使用 for 循环并直接将其输入到 $strOut 中尝试了以下操作。我真的不知道这是否是你应该这样做的(显然不是,因为它不起作用 -> 在第 42 行 [$tbl 行] 给出错误意外'for')并且找不到解决方法.如何使数组数据显示在 file_put_content 的输出中,以免产生错误?

$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$note = $_POST['note'];

$tbl = for ($i = 0; $i < count($ingredient); $i++) {
    echo '<tr>';
    echo '<td>' . $ingredient[$i] . '</td>';
    echo '<td>' . $amount[$i] . '</td>';
    echo '<td>' . $note[$i] . '</td>';
    echo '</tr>';
}

$strOut =   '<!DOCTYPE html>'
        .   '<table class="recipe-ingredients">'
        .   '<tr>'
        .   '<th class="table-th-large">Ingredient</th>'
        .   '<th class="table-th-medium">Amount</th>'
        .   '<th class="table-th-xl">Note</th>'
    // User input tr goes here
        .   $tbl
        .   '</table>';
    // Closing html tags after

file_put_contents("example.php", $strOut);

任何建议或解决方法将不胜感激。

【问题讨论】:

    标签: php html forms post


    【解决方案1】:
    $tbl = '';
    for ($i = 0; $i < count($ingredient); $i++) {
            $tbl.= '<tr>';
            $tbl.= '<td>' . $ingredient[$i] . '</td>';
            $tbl.= '<td>' . $amount[$i] . '</td>';
            $tbl.= '<td>' . $note[$i] . '</td>';
            $tbl.= '</tr>';
        }
    

    使用这个。 顺便说一句,您忘记在标题行中添加结束 &lt;/tr&gt;

    【讨论】:

    • 完美!感谢您的帮助。
    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多